Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/234.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP:使用条件连接数组_Php_Arrays - Fatal编程技术网

PHP:使用条件连接数组

PHP:使用条件连接数组,php,arrays,Php,Arrays,我正在尝试读取会话变量,创建数组并连接它们: 会话变量示例: Array ( [0] => Array ( [idArticle] => 224 [ntypeArticle] => 1 ) [1] => Array ( [idArticle] => 556 [ntypeArticle] => 2 ) [2] => Array ( [id

我正在尝试读取会话变量,创建数组并连接它们:

会话变量示例:

Array
(
[0] => Array
    (
        [idArticle] => 224
        [ntypeArticle] => 1
    )
[1] => Array
    (
        [idArticle] => 556
        [ntypeArticle] => 2
    )
[2] => Array
    (
        [idArticle] => 312
        [ntypeArticle] => 1
    )   
)
$type1 = array();
$type2= array();
$typeAll = array();
foreach($_SESSION['cart'] as $item)
{

    if ($item['ntypeArticle'] == 1) {
        $type1= array ( "Type" => '1', );
    } else {
        $type2= array ( "Type" => '2', );
    }
    array_push($typeAll , $type1 , $type2);
}
Array
(
[0] => Array
(
    [type] => 1
)
[1] => Array
(
    [type] => 2
)
[2] => Array
(
    [type] => 1
)
)
我需要逐一阅读此数组,并通过“
ntypeArticle
”创建数组

如果
ntypeArticle=1
创建数组1

如果
ntypeArticle=2
创建数组2

我的代码:

Array
(
[0] => Array
    (
        [idArticle] => 224
        [ntypeArticle] => 1
    )
[1] => Array
    (
        [idArticle] => 556
        [ntypeArticle] => 2
    )
[2] => Array
    (
        [idArticle] => 312
        [ntypeArticle] => 1
    )   
)
$type1 = array();
$type2= array();
$typeAll = array();
foreach($_SESSION['cart'] as $item)
{

    if ($item['ntypeArticle'] == 1) {
        $type1= array ( "Type" => '1', );
    } else {
        $type2= array ( "Type" => '2', );
    }
    array_push($typeAll , $type1 , $type2);
}
Array
(
[0] => Array
(
    [type] => 1
)
[1] => Array
(
    [type] => 2
)
[2] => Array
(
    [type] => 1
)
)
但这会创建空数组

想要的输出:

Array
(
[0] => Array
    (
        [idArticle] => 224
        [ntypeArticle] => 1
    )
[1] => Array
    (
        [idArticle] => 556
        [ntypeArticle] => 2
    )
[2] => Array
    (
        [idArticle] => 312
        [ntypeArticle] => 1
    )   
)
$type1 = array();
$type2= array();
$typeAll = array();
foreach($_SESSION['cart'] as $item)
{

    if ($item['ntypeArticle'] == 1) {
        $type1= array ( "Type" => '1', );
    } else {
        $type2= array ( "Type" => '2', );
    }
    array_push($typeAll , $type1 , $type2);
}
Array
(
[0] => Array
(
    [type] => 1
)
[1] => Array
(
    [type] => 2
)
[2] => Array
(
    [type] => 1
)
)
试试这个

$newOp1 = array()
$newOp2 = array()
foreach($_SESSION['cart'] as $item){
    if($item["ntypeArticle"] == 1){
       $newOp1[]['type'] = $item["ntypeArticle"]
    }else{
       $newOp2[]['type'] = $item["ntypeArticle"]
    }
}
print_r($newOp1);
print_r($newOp2);
试试这个

$newOp1 = array()
$newOp2 = array()
foreach($_SESSION['cart'] as $item){
    if($item["ntypeArticle"] == 1){
       $newOp1[]['type'] = $item["ntypeArticle"]
    }else{
       $newOp2[]['type'] = $item["ntypeArticle"]
    }
}
print_r($newOp1);
print_r($newOp2);

根据您提供的信息,您只需将
nTypeArticle
提取为
type
元素。就这些

//array from your example
$inputArray = array(
    array(
        'idArticle' => 224,
        'nTypeArticle' => 1
    ),
    array(
        'idArticle' => 556,
        'nTypeArticle' => 2
    ),
    array(
        'idArticle' => 312,
        'nTypeArticle' => 1
    ),
);

$outputArray = array_map(function($inputElement) {
    return array('type' => $inputElement['nTypeArticle']);
}, $inputArray);

var_dump($outputArray);
//Output (the same as yours):
//array (size=3)
//  0 => 
//    array (size=1)
//      'type' => int 1
//  1 => 
//    array (size=1)
//      'type' => int 2
//  2 => 
//    array (size=1)
//      'type' => int 1

根据您提供的信息,您只需将
nTypeArticle
提取为
type
元素。就这些

//array from your example
$inputArray = array(
    array(
        'idArticle' => 224,
        'nTypeArticle' => 1
    ),
    array(
        'idArticle' => 556,
        'nTypeArticle' => 2
    ),
    array(
        'idArticle' => 312,
        'nTypeArticle' => 1
    ),
);

$outputArray = array_map(function($inputElement) {
    return array('type' => $inputElement['nTypeArticle']);
}, $inputArray);

var_dump($outputArray);
//Output (the same as yours):
//array (size=3)
//  0 => 
//    array (size=1)
//      'type' => int 1
//  1 => 
//    array (size=1)
//      'type' => int 2
//  2 => 
//    array (size=1)
//      'type' => int 1

你所需要的只是一件简单的事情:

$out = [];
foreach($_SESSION['cart'] as $k => $item)
{
    $out[$k] = ['Type' => $item['ntypeArticle']];
}

现在,如果您输出
$out
变量,您就可以得到您所需要的。

您所需要的只是一件简单的事情:

$out = [];
foreach($_SESSION['cart'] as $k => $item)
{
    $out[$k] = ['Type' => $item['ntypeArticle']];
}
$output = new array();
$input = array(
array(
    "idArticle" => 224,
    "nTypeArticle" => 1
    ),
    array(
        "idArticle" => 556,
        "nTypeArticle" => 2
    ),
    array(
        "idArticle" => 312,
        "nTypeArticle" => 1
    )
);

foreach($input as $article) {
    array_push($output, new array("type"=>$article["nTypeArticle"]));
}
print_r($output);
现在,如果您输出
$out
变量,您将得到所需的内容

$output = new array();
$input = array(
array(
    "idArticle" => 224,
    "nTypeArticle" => 1
    ),
    array(
        "idArticle" => 556,
        "nTypeArticle" => 2
    ),
    array(
        "idArticle" => 312,
        "nTypeArticle" => 1
    )
);

foreach($input as $article) {
    array_push($output, new array("type"=>$article["nTypeArticle"]));
}
print_r($output);
这就是你想要的东西。 (代码尚未测试)

这就是你想要的东西。
(代码未经测试)

您似乎没有对$type1$type2进行任何操作,在您为它们分配了值/数组之后,$listArticleResult、$listArticlePax、$listArticlePluseUrsPax从何而来,
$ListArticlePlusiersPax
是否在某处定义过?
$type1=array(“Type”=>“1”)
将创建一个名为
$type1
的标量变量,但只包含循环
$type1[]=array(“Type”=>1',)中的最后一个条目
将创建一个type1’s数组,但需要注意的是:您所说的新数组实际上并没有实现超出原始数组的任何功能。并且可以从当前形式的原始数组中获得完全相同的信息。你确定这实际上实现了什么吗?当然,它会创建空值,每次循环时都要插入
$type1
$type2
,但每个循环只设置一个变量,这意味着在if子句中为
$type1
$type2
都指定了值之前,您将继续在
$typeAll
数组中获取空值。在为
$type1
$type2
分配值/数组后,您似乎没有对它们执行任何操作,
$listArticleResult、$listArticlePax、$listArticlePluseUrspax
来自何处?是否
$listArticleResult
$listArticlePax
$ListArticlePlusierPax
是否在某个地方定义过?
$type1=array(“Type”=>1',)
将创建一个名为
$type1
的标量变量,但只包含循环
$type1[]=array(“Type”=>1',)中的最后一个条目
将创建一个type1’s数组,但需要注意的是:您所说的新数组实际上并没有实现超出原始数组的任何功能。并且可以从当前形式的原始数组中获得完全相同的信息。你确定这实际上实现了什么吗?当然,它会创建空值,每次循环时都要插入
$type1
$type2
,但每个循环只设置一个变量,这意味着在if子句中为
$type1
$type2
都指定了值之前,您将继续在
$typeAll
数组中获取空值。