我有一个问题。如何从此PHP结果中获取值?做print_r($ result);我得到以下内容:
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
)
看看其他帖子我无法弄清楚。我看了看:How to access a property of an object (stdClass Object) member/element of an array?。这给了我一点温柔,但在这里我有: 对象内数组中的对象
我迷路了。我想呼应卖家,运费和价格。谁可以对此有所了解?
答案 0 :(得分:2)
您必须使用箭头语法->
和数组语法[]
来访问属性,因为$ 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";
访问特定的索引“结果”
echo $result->results[1]->seller . "<br />";
echo $result->results[1]->shipping . "<br />";
echo $result->results[1]->price . "<br />";
或全部循环
foreach ($result->results as $k => $v) {
echo $v->seller . "<br />";
echo $v->shipping . "<br />";
echo $v->price . "<br />";
}
答案 1 :(得分:2)
您可以使用以下方式访问您所需的值:
$i = 0; //change or use loop
$result->results[$i]->seller //or price, shipping etc
答案 2 :(得分:1)
如果$ value错误尝试$ key
,你可以用foreach遍历它 foreach($result->results as $key => $value){
echo $value->shipping;
echo $value->price;
}
答案 3 :(得分:0)
假设您的类在变量$x
echo $x->results[0]->seller
这里有一个重要的警告。您的班级必须将这些设置为public
,否则您需要overload function __get()
来检索该值。否则,你必须有一个函数来获取值并返回它。