Php数组用键推

时间:2017-08-07 16:44:34

标签: php arrays array-push

以下是我想要输出的内容:

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

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

)

我的实际代码如下:

enter image description here

限制:enter image description here

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;

                }

我尝试了所有的可能性,但我认为我用完了解决方案。

2 个答案:

答案 0 :(得分:2)

试试这个

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]);

                    }

答案 1 :(得分:0)

为什么两个指数在:

中相同
$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;