0

I have a Question. How do I get the values from this PHP result? Doing print_r($result); I get the following:

stdClass Object 
( 
    [offset] => 0 
    [results] => Array 
    ( 
        [0] => stdClass Object 
        ( 
            [shipping] => Kostenlose Lieferung. 
            [price] => EUR 8,03 
        ) 
        [1] => stdClass Object 
        ( 
            [seller] => Zooster 
            [shipping] => EUR 3,00 
            [price] => EUR 9,06 
        ) 
    ) 
[cookies] => Array 
    ( 
        [0] => x-wl 
        [1] => session-id-time="2082754801l" 
        [2] => session-id="2510769"; 
    ) 
[connectorVersionGuid] => lalala 
[connectorGuid] => blavla 
[pageUrl] => http://www.lala.com 

)

Looking at other posts I cannot figure it out. I looked at: How to access a property of an object (stdClass Object) member/element of an array?. This gives me a bit of anwser but here I have: an object within an array within an object

I am lost. I want to echo the seller, shipping and price. Who can shed some light on this?

Community
  • 1
  • 1
Chris
  • 1
  • 1

4 Answers4

2

You have to access the attributes using the arrow syntax -> and the array syntax [] as $result combines objects and arrays together

//Creating version identical to your print_r
$s1 = new stdClass();
$s1->shipping = "Kostenlose Lieferung.";
$s1->price = "EUR 8,03";

$s2 = new stdClass();
$s2->seller = "Zooster";
$s2->shipping = "EUR 3,00";
$s2->price = "EUR 9,06";

$result = new stdClass();
$result->offset = 0;
$result->results = array($s1, $s2);
$result->cookies = array("x-wl", "session-id-time=\"2082754801l\"", "session-id-=\"2510769\"");
$result->connectorVersionGuid = "lalala";
$result->connectorGuid = "blavla";
$result->pageUrl = "http://www.lala.com";

Access particular indexed 'results'

echo $result->results[1]->seller . "<br />";
echo $result->results[1]->shipping . "<br />";
echo $result->results[1]->price . "<br />";

Or loop through them all

foreach ($result->results as $k => $v) {
    echo $v->seller . "<br />";
    echo $v->shipping . "<br />";
    echo $v->price . "<br />";
}
iswinky
  • 1,951
  • 3
  • 16
  • 47
2

You can access the values you are after by using:

$i = 0; //change or use loop

$result->results[$i]->seller //or price, shipping etc
1

You can probably loop through it with a foreach, if $value errors try $key

   foreach($result->results as $key => $value){
       echo $value->shipping;
       echo $value->price;
    }
Sjoerd de Wit
  • 2,353
  • 5
  • 26
  • 45
0

Assuming your class is instanced in the variable $x

echo $x->results[0]->seller

There's a major caveat here. Your class has to have these set to public, or you need to have an overload function __get() to retrieve the value. Otherwise, you will have to have a function to get the value and return it.

Machavity
  • 30,841
  • 27
  • 92
  • 100