简体   繁体   中英

php multidimensional array loop

I'm working with SunShop attempting to build a custom reporting script for my client. I'm having a difficult time understanding how to run a foreach() statement to pull out any chosen info from this array. For instance, I would like to pull out each option name & value.

Goal:

echo $data['options']['name']
echo $data['options']['value']

I've tried several methods of implementing foreach() to loop through and display my results, but it fails every time either telling me I'm not unserializing correctly, or that there's an undefined object. Can any of you shed light on this? I certainly don't know enough about arrays.

Also, I think it's worth mentioning, that I'm not dealing with sessions. I'm building this outside of SunShop just to run on occasion to pull reports when requested.

How I get my array:

<?php 
$array=unserialize(base64_decode($data));
var_dump($array);
?>

Array dump:

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)

You just need to learn the difference between objects and arrays. $array according to your var_dump is an object not an array. $options is an array of objects.

$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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM