我正在连接到一个返回此嵌套对象值的API。
我如何迭代每个["数字:protected"]?这对我来说是新的,我只想返回"数字:protected"。
Array
(
[0] => SDK2go\seat2go Object
(
[status:protected] => Vacant
[number:protected] => D,10,A,STR3,ROOM
[passenger:protected] =>
[price:protected] =>
)
[1] => SDK2go\seat2go Object
(
[status:protected] => Vacant
[number:protected] => D,15,A,STR3,ROOM
[passenger:protected] =>
[price:protected] =>
)
[2] => SDK2go\seat2go Object
(
[status:protected] => Vacant
[number:protected] => D,16,A,STR3,ROOM
[passenger:protected] =>
[price:protected] =>
)
[3] => SDK2go\seat2go Object
(
[status:protected] => Vacant
[number:protected] => D,17,A,STR3,ROOM
[passenger:protected] =>
[price:protected] =>
)
[4] => SDK2go\seat2go Object
(
[status:protected] => Vacant
[number:protected] => D,18,A,STR3,ROOM
[passenger:protected] =>
[price:protected] =>
)
[5] => SDK2go\seat2go Object
(
[status:protected] => Vacant
[number:protected] => D,19,A,STR3,ROOM
[passenger:protected] =>
[price:protected] =>
)
)
我有什么东西但没有显示出来:
foreach($sList as $list){
echo $list['number:protected'];
}
请帮忙。提前谢谢。
答案 0 :(得分:0)
所以这里发生的是你的API在数组中以对象形式返回类的实例。要模仿它,请查看以下代码:
<?php
namespace SDK2go;
class seat2go
{
protected $status;
protected $number;
protected $passenger;
protected $price;
public function __construct($status, $number)
{
$this->status = $status;
$this->number = $number;
}
public function get_status()
{
echo $this->status;
}
public function get_number()
{
echo $this->number;
}
}
$arr = array(
new seat2go("Vacant", "D,10,A,STR3,ROOM"),
new seat2go("Vacant", "D,15,A,STR3,ROOM"),
new seat2go("Vacant", "D,16,A,STR3,ROOM")
);
echo "<pre>";
print_r($arr);
echo "</pre>";
以上内容将为您提供print_r
个结果。因此,为了访问这些属性,您必须调用类应该具有的getter
函数。如此:
foreach ($arr as $a) {
echo $a->get_status();
echo $a->get_number();
}
希望这会有所帮助。