Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iterating through multi dimensional arrays

I am trying to get the item id, and then all option_name/option_values within that item id. So I end up with, ID: 123, Color: Blue, Size: 6. ID: 456, Color: Yellow, Size: 8. However I am getting the correct item ID, but the option_name/option_value isn't coming through correctly, either blank or just one random letter.

Here's my code that doesn't work,

foreach($itemlist as $item)
{
   echo $item['ID'];

   foreach($item as $option)
   { 
       echo $option['option_name'];
       echo $option['option_value'];
   }
 }

Where $itemlist looks like this:

Array
(
    [1] => Array
        (
            [ID] => 123
            [QTY] => 1
            [MODEL] => sdfsd
            [IMAGE] => 
            [1] => Array
                (
                    [option_name] => Color
                    [option_value] => Blue
                    [option_price] => 0.0000
                )

            [2] => Array
                (
                    [option_name] => Size
                    [option_value] => 6
                    [option_price] => 0.0000
                )

            [price] => 0
        )

    [2] => Array
        (
            [ID] => 456
            [QTY] => 0
            [MODEL] => gsdfgd
            [IMAGE] => 
            [1] => Array
                (
                    [option_name] => Color
                    [option_value] => Yellow
                    [option_price] => 0.0000
                )

            [2] => Array
                (
                    [option_name] => Size
                    [option_value] => 8
                    [option_price] => 0.0000
                )

            [price] => 0
        )

)
like image 980
user1155594 Avatar asked Dec 15 '22 08:12

user1155594


2 Answers

Basically, you're looping over the $item array, which looks like this:

array(7) {
   ["ID"]=>string(6) "123"
   ["QTY"]=>string(1) "1"
   ["MODEL"]=>string(11) "sdfsd"
   ["IMAGE"]=>string(0) ""
   [1]=>
       array(3) {
           ["option_name"]=>string(8) "Color"
           ["option_value"]=>string(10) "Blue"
           ["option_price"]=>string(6) "0.0000"
      }

So on the first iteration, $option will be 123, trying to access '123'['option_name'] will issue a warning. What you actually wanted to do is this:

foreach($item[1] as $key => $option)
{
    if ($key !== 'option_price')
    {
        echo $option;
    }
}
//or:
echo $item['ID'], $item[1]['option_name'], $item['option_value'];

That's why your code doesn't produce the desired result.
If the sub-array doesn't always have 1 as a key, try:

foreach($item as $foo)
{
    if (is_array($foo))
    {
        echo $foo['option_name'], $foo['option_value'];
        break;//we have what we needed, no need to continue looping.
    }
}

Here's the most generic approach to get all options (irrespective of how many)

foreach($itemlist as $item)
{
    echo $item['ID'];
    foreach($item as $sub)
    {
        if (is_array($sub))
        {
            foreach($sub as $key => $option)
            {
                echo $key, ' => ', $option;
            }
        }
    }
}

But seeing as your options arrays look like they all have numeric indexes, you could just as well try this:

foreach($itemlist as $item)
{
    echo $item['ID'];
    for ($i=1;isset($item[$i]);$i++)
    {
        foreach($item[$i] as $key => $option)
        {
            echo $key, ' => ', $option;
        }
    }
}

You could replace the for loop with:

$i=0;//or $i = 1
while(isset($item[++$i]))// or isset($item[$i++]), if $i is 1
like image 157
Elias Van Ootegem Avatar answered Jan 02 '23 23:01

Elias Van Ootegem


This is because your array contains the POSSIBILITY of those keys existing, not guaranteed. There's a few different approaches you could take, such as checking if the key is numeric (seems only numeric keys have the correct array of keys set). But since i dont know your dataset, probably the best universal method is to change your code to check if the key's are set, before trying to use them. So..

foreach($itemlist as $item) {

 foreach($item as $key => $value) {

      if(is_array($value) && array_key_exists('option_name', $value) {

           //--- Your option array is available in here via $value['option_x']

      }

 }

}
like image 37
Lee Avatar answered Jan 03 '23 01:01

Lee