-1

I have created an array using a foreach loop and the result loops like this:

array(8) { 
    [0]=> array(3) { 
        ["shortdesc"]=> object(SimpleXMLElement)#63 (1) {...}
        ["longdesc"]=> object(SimpleXMLElement)#58 (1) {...} 
        ["price"]=> object(SimpleXMLElement)#64 (1) {...} 
    } 
    [1]=> array(3) { 
        ["shortdesc"]=> object(SimpleXMLElement)#67 (1) {...} 
        ["longdesc"]=> object(SimpleXMLElement)#62 (1) {...} 
        ["price"]=> object(SimpleXMLElement)#68 (1) {...} 
    }  
    (...)
}

Now I want to loop through that array and output each item.

I thought this would work ($optionsArray is the array above):

foreach ($optionsArray as $key => $sortedOption) {
  $longDesc = $sortedOption->longdesc;
  $shortDesc = $sortedOption->shortdesc;
  $price = $sortedOption->price;
  echo "<li>$shortDesc:</li>";
}

but it gives me:

Notice: Trying to get property of non-object in

Where am I going with this?

EDIT:

Now found this usage:

foreach ($optionsArray as $innerArray) {
  foreach ($innerArray as $value) {
    echo "<li>$value</li>";
  } 
}

Which works but outputs all of the array elements, how can I choose which elements and assign them to a variable?

Lovelock
  • 7,689
  • 19
  • 86
  • 186

1 Answers1

-1

'->' is only for objects, not applicable to arrays. Use ['key'] instead of ->key.

Vladyslav Savchenko
  • 1,282
  • 13
  • 10