简体   繁体   English

在对象内的数组内的stdClass对象中回显值

[英]echo a value in an stdClass Object within an array within an object

I have a Question. 我有个问题。 How do I get the values from this PHP result? 如何从此PHP结果中获取值? Doing print_r($result); 做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? 我看了一下: 如何访问对象(stdClass对象)的属性或数组的元素? . 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? 谁能对此有所启发?

You have to access the attributes using the arrow syntax -> and the array syntax [] as $result combines objects and arrays together 您必须使用箭头语法->和数组语法[]访问属性,因为$ result将对象和数组组合在一起

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

You can access the values you are after by using: 您可以使用以下方法访问所需的值:

$i = 0; //change or use loop

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

You can probably loop through it with a foreach, if $value errors try $key 如果$ value错误尝试$ key,则可以使用foreach遍历它

   foreach($result->results as $key => $value){
       echo $value->shipping;
       echo $value->price;
    }

Assuming your class is instanced in the variable $x 假设您的课程在$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. 您的类必须将这些设置为public ,或者您需要具有重载函数 __get()来检索值。 Otherwise, you will have to have a function to get the value and return it. 否则,您将必须具有一个函数来获取值并将其返回。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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