• <bdo id='hDwWw'></bdo><ul id='hDwWw'></ul>
    <legend id='hDwWw'><style id='hDwWw'><dir id='hDwWw'><q id='hDwWw'></q></dir></style></legend>

    <small id='hDwWw'></small><noframes id='hDwWw'>

  1. <i id='hDwWw'><tr id='hDwWw'><dt id='hDwWw'><q id='hDwWw'><span id='hDwWw'><b id='hDwWw'><form id='hDwWw'><ins id='hDwWw'></ins><ul id='hDwWw'></ul><sub id='hDwWw'></sub></form><legend id='hDwWw'></legend><bdo id='hDwWw'><pre id='hDwWw'><center id='hDwWw'></center></pre></bdo></b><th id='hDwWw'></th></span></q></dt></tr></i><div id='hDwWw'><tfoot id='hDwWw'></tfoot><dl id='hDwWw'><fieldset id='hDwWw'></fieldset></dl></div>

      <tfoot id='hDwWw'></tfoot>

      迭代多维数组

      时间:2023-09-21

      <i id='r1i7q'><tr id='r1i7q'><dt id='r1i7q'><q id='r1i7q'><span id='r1i7q'><b id='r1i7q'><form id='r1i7q'><ins id='r1i7q'></ins><ul id='r1i7q'></ul><sub id='r1i7q'></sub></form><legend id='r1i7q'></legend><bdo id='r1i7q'><pre id='r1i7q'><center id='r1i7q'></center></pre></bdo></b><th id='r1i7q'></th></span></q></dt></tr></i><div id='r1i7q'><tfoot id='r1i7q'></tfoot><dl id='r1i7q'><fieldset id='r1i7q'></fieldset></dl></div>

        • <tfoot id='r1i7q'></tfoot>
          <legend id='r1i7q'><style id='r1i7q'><dir id='r1i7q'><q id='r1i7q'></q></dir></style></legend>
            <tbody id='r1i7q'></tbody>
            <bdo id='r1i7q'></bdo><ul id='r1i7q'></ul>

                <small id='r1i7q'></small><noframes id='r1i7q'>

                本文介绍了迭代多维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我正在尝试获取项目 ID,然后是该项目 ID 中的所有 option_name/option_values.所以我最终得到了 ID:123,颜色:蓝色,尺寸:6.ID:456,颜色:黄色,尺寸:8.但是我得到了正确的项目 ID,但是 option_name/option_value 没有正确通过, 要么是空白,要么只是一个随机字母.

                这是我的代码不起作用,

                foreach($itemlist as $item){回声 $item['ID'];foreach($item 作为 $option){回声 $option['option_name'];回声 $option['option_value'];}}

                $itemlist 看起来像这样:

                <前>大批([1] => 数组([ID] => 123[数量] => 1[模型] => sdfsd[图像] =>[1] => 数组([option_name] => 颜色[option_value] => 蓝色[option_price] => 0.0000)[2] => 数组([option_name] => 尺寸[option_value] => 6[option_price] => 0.0000)[价格] => 0)[2] => 数组([ID] => 456[数量] => 0[模型] => gsdfgd[图像] =>[1] => 数组([option_name] => 颜色[option_value] => 黄色[option_price] => 0.0000)[2] => 数组([option_name] => 尺寸[option_value] => 8[option_price] => 0.0000)[价格] => 0))

                解决方案

                基本上,你在循环 $item 数组,它看起来像这样:

                array(7) {[ID"]=> 字符串(6)123"[数量"]=> 字符串(1)1"[模型"]=> 字符串(11)sdfsd"[图像"]=> 字符串(0)"[1]=>数组(3){["option_name"]=>string(8) "颜色"["option_value"]=>string(10) "蓝色"["option_price"]=>string(6) "0.0000"}

                因此在第一次迭代时,$option 将是 123,尝试访问 '123'['option_name'] 将发出一个警告.你真正想做的是:

                foreach($item[1] as $key => $option){如果 ($key !== 'option_price'){回声 $option;}}//或者:echo $item['ID'], $item[1]['option_name'], $item['option_value'];

                这就是为什么你的代码没有产生想要的结果.
                如果子数组并不总是将 1 作为键,请尝试:

                foreach($item as $foo){如果 (is_array($foo)){echo $foo['option_name'], $foo['option_value'];break;//我们有我们需要的,不需要继续循环.}}

                这是获取所有选项(无论有多少)的最通用方法

                foreach($itemlist as $item){回声 $item['ID'];foreach($item 作为 $sub){如果(is_array($sub)){foreach($sub as $key => $option){echo $key, ' =>', $option;}}}}

                但是看到您的选项数组看起来都具有数字索引,您也可以试试这个:

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

                您可以将 for 循环替换为:

                $i=0;//或$i = 1while(isset($item[++$i]))//或 isset($item[$i++]),如果 $i 是 1

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

                解决方案

                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
                

                这篇关于迭代多维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:在 simpleXMLElement 的 foreach 循环中获取子索引 下一篇:使用“optgroup"选项选择下拉 PHP foreach 循环

                相关文章

                最新文章

                1. <i id='9ukeN'><tr id='9ukeN'><dt id='9ukeN'><q id='9ukeN'><span id='9ukeN'><b id='9ukeN'><form id='9ukeN'><ins id='9ukeN'></ins><ul id='9ukeN'></ul><sub id='9ukeN'></sub></form><legend id='9ukeN'></legend><bdo id='9ukeN'><pre id='9ukeN'><center id='9ukeN'></center></pre></bdo></b><th id='9ukeN'></th></span></q></dt></tr></i><div id='9ukeN'><tfoot id='9ukeN'></tfoot><dl id='9ukeN'><fieldset id='9ukeN'></fieldset></dl></div>

                  <legend id='9ukeN'><style id='9ukeN'><dir id='9ukeN'><q id='9ukeN'></q></dir></style></legend>
                  1. <small id='9ukeN'></small><noframes id='9ukeN'>

                      <bdo id='9ukeN'></bdo><ul id='9ukeN'></ul>
                  2. <tfoot id='9ukeN'></tfoot>