[英]php multidimensional array loop
我正在与SunShop合作,尝试为我的客户构建自定义报告脚本。 我很难理解如何运行foreach()语句以从此数组中提取所有选定的信息。 例如,我想提取每个选项的名称和值。
目标:
echo $data['options']['name']
echo $data['options']['value']
我已经尝试了几种实现foreach()的方法来循环显示我的结果,但是每次告诉我我没有正确地反序列化,或者有一个未定义的对象,它都会失败。 你们中的任何一个都可以阐明这一点吗? 我当然对数组不够了解。
另外,我认为值得一提的是,我不处理会议。 我在SunShop之外构建此文件,只是为了在需要时偶尔运行以提取报告。
我如何获得阵列:
<?php
$array=unserialize(base64_decode($data));
var_dump($array);
?>
阵列转储:
object(__PHP_Incomplete_Class)[1]
public '__PHP_Incomplete_Class_Name' => string 'item' (length=4)
public 'id' => int 655
public 'quantity' => float 3
public 'options' =>
array
0 =>
object(__PHP_Incomplete_Class)[2]
public '__PHP_Incomplete_Class_Name' => string 'option' (length=6)
public 'id' => string '487' (length=3)
public 'product' => string '655' (length=3)
public 'name' => string 'Choose Brand' (length=12)
public 'value' => string 'Brand Name' (length=10)
public 'valueid' => string '2026' (length=4)
public 'weight' => string '0' (length=1)
public 'price' => string '0' (length=1)
public 'desc' => string '' (length=0)
public 'sku' => string '' (length=0)
1 =>
object(__PHP_Incomplete_Class)[3]
public '__PHP_Incomplete_Class_Name' => string 'option' (length=6)
public 'id' => string '488' (length=3)
public 'product' => string '655' (length=3)
public 'name' => string 'Choose Size & Color' (length=19)
public 'value' => string 'Chocolate - Medium' (length=18)
public 'valueid' => string '2022' (length=4)
public 'weight' => string '0' (length=1)
public 'price' => string '0' (length=1)
public 'desc' => string '' (length=0)
public 'sku' => string '' (length=0)
public 'regid' => string '' (length=0)
您只需要了解对象和数组之间的区别。 根据您的var_dump, $array
是一个对象而不是数组。 $options
是一个对象数组。
$data = array();
foreach($array->options as $option) {
$data[] = array(
'name' => $option->name,
'value' => $option->value,
);
//of if needed instead of storing these values in $data array you
//can just echo these values.
//echo $option->name;
//echo $option->value;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.