Php 键数组推送

Php Array push with key

这是我要输出的内容:

Array
(
    [0] => Array
        (
            [restriction_type_code] => CALORICONTROL
            [restriction_detail_code] => 3000CAL
        )

    [1] => Array
        (
            [restriction_type_code] => GLUTENFREE
            [restriction_detail_code] => NR
        )

)

我的实际代码如下所示:

限制:

foreach ($restriction as   $value) 
                {
                    $itemSplit =  explode("||", $value);
                    $itemSplit1 = explode("|", $itemSplit[0]);
                    $itemSplit2 = explode("|", $itemSplit[0]); 

                    $arrOrderDiet['restriction_type_code'][] = $itemSplit1 //CALORICONTROL;
                    $arrOrderDiet['restriction_detail_code'][] = $itemSplit2//3000CAL;

                }

我尝试了所有的可能性,但我认为我 运行 没有解决方案。

试试这个

foreach ($restriction as   $value) 
                {
                    $itemSplit =  explode("||", $value);
                    $itemSplit1 = explode("|", $itemSplit[0]);
                    $itemSplit2 = explode("|", $itemSplit[1]); 

                    $arrOrderDiet[] = array('restriction_type_code' => $itemSplit1, 'restriction_detail_code' => $itemSplit2);

                }

编辑:

foreach ($restriction as   $value) 
                    {
                        $itemSplit =  explode("||", $value);
                        $itemSplit1 = explode("|", $itemSplit[0]);
                        $itemSplit2 = explode("|", $itemSplit[1]); 

                        $arrOrderDiet[] = array('restriction_type_code' => $itemSplit1[0], 'restriction_detail_code' => $itemSplit2[2]);

                    }

为什么两个索引相同:

$itemSplit1 = explode("|", $itemSplit[0]);
$itemSplit2 = explode("|", $itemSplit[0]);

不应该是这样吗:

$itemSplit1 = explode("|", $itemSplit[0]);
$itemSplit2 = explode("|", $itemSplit[1]); 

加上:

$tmp['restriction_type_code'] = $itemSplit1[0];
$tmp['restriction_detail_code'] = $itemSplit2[0];
$arrOrderDiet[] = $tmp;