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?

3
  • 3
    echo $result->results[1]->seller; Commented Sep 5, 2014 at 14:04
  • @JimL Post as an answer :) Commented Sep 5, 2014 at 14:06
  • @ʰᵈˑ no time to write a proper answer atm Commented Sep 5, 2014 at 14:07

4 Answers 4

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 />";
}
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.