沃梦达 / 编程问答 / php问题 / 正文

迭代多维数组

iterating through multi dimensional arrays(迭代多维数组)

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

问题描述

我正在尝试获取项目 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

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

本文标题为:迭代多维数组

基础教程推荐