1

我正在与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)
4

1 回答 1

2

您只需要了解对象和数组之间的区别。$array根据你的 var_dump 是一个对象而不是一个数组。$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;
}
于 2013-06-27T14:10:08.057 回答