Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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)
share|improve this question

1 Answer 1

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;
}
share|improve this answer
    
Thanks for pointing that out. I had no idea. Are you recommending your code to work as-is, or after I convert object to array? I tried it as-is and it didn't work. –  trainwreck Jun 28 '13 at 1:07
    
I can't say my code will work as is, because I don't know what your end result should look like. I was just showing you how to get the names and values out of your object/array. –  Pitchinnate Jun 28 '13 at 12:34

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.