kevin_marcus
kevin_marcus

Reputation: 277

Php Array push with key

Here is what I want to output:

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

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

)

and my actual code looks like this:

enter image description here

restriction : 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;

                }

Im trying all the possibilities but I think i ran out of solutions.

Upvotes: 0

Views: 427

Answers (2)

Deblugger
Deblugger

Reputation: 153

Try this

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

                }

Edit:

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

                    }

Upvotes: 2

Martin Perry
Martin Perry

Reputation: 9527

Why do you have both indices the same in:

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

Shouldn't it be like this:

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

Plus:

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

Upvotes: 0

Related Questions