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'];}}
<前>大批([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))
$itemlist
看起来像这样:
基本上,你在循环 $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
这篇关于迭代多维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:迭代多维数组
基础教程推荐
- phpmyadmin 错误“#1062 - 密钥 1 的重复条目‘1’" 2022-01-01
- 如何在 Symfony 和 Doctrine 中实现多对多和一对多? 2022-01-01
- 在 CakePHP 2.0 中使用 Html Helper 时未定义的变量 2021-01-01
- 在 yii2 中迁移时出现异常“找不到驱动程序" 2022-01-01
- 使用 PDO 转义列名 2021-01-01
- PHP 守护进程/worker 环境 2022-01-01
- 找不到类“AppHttpControllersDB",我也无法使用新模型 2022-01-01
- 如何在 XAMPP 上启用 mysqli? 2021-01-01
- Doctrine 2 - 在多对多关系中记录更改 2022-01-01
- HTTP 与 FTP 上传 2021-01-01