0

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.

3
  • 1
    Ok post your source pls Commented Aug 7, 2017 at 16:45
  • We don't know what your data looks like mate Commented Aug 7, 2017 at 16:49
  • The output array should be like the one i posted on the top. Commented Aug 7, 2017 at 16:53

2 Answers 2

2

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

                    }
Sign up to request clarification or add additional context in comments.

5 Comments

it returns this : [0] => Array ( [restriction_type_code] => Array ( [0] => CALORICONTROL [1] => Calorie controlled ) [restriction_detail_code] => Array ( [0] => CALORICONTROL [1] => Calorie controlled ) )
still not the answer I want to be. ~sigh
Don't you think you posted a bit too early considering you have no idea what the data actually looks like?
Yep... Can you copy print_r($itemSplit1) and print_r($itemSplit2) output?
You updated with the output of $restrictions, i updated my answer anyways
0

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;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.