1

I am connecting to an API which returns this nested object value.

How can i iterate each ["number: protected"] ? This is something new to me, i only want to return the "number: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] => 
    )

 )

I have something but nothing was shown:

 foreach($sList as $list){
   echo $list['number:protected'];  
 }  

Please help. Thanks in advance.

user3651476
  • 171
  • 1
  • 9
  • 20
  • 2
    How the `SDK2go\seat2go` class looks like? is there any `getNumber()` method within? if so, call it like `$list->getNumber()`.. You should search for any public method to see what to use.. – Mihai Matei Mar 21 '16 at 12:55
  • 1
    these are instances of namespaced classes in an array of objects. You are trying to access protected properties inside a class. This will not be an option as protected properties are.. well protected.. check this out: http://stackoverflow.com/questions/4361553/php-public-private-protected ... as @MateiMihai mentioned above, there should be a _"getter"_ method that will allow you to get these properties. – CodeGodie Mar 21 '16 at 12:57
  • okay got . .echo $obj->getNumber(); – user3651476 Mar 21 '16 at 13:05

1 Answers1

1

So whats happening here is that your API is returning instances of a class in object form inside an array. To mimic it check out the following code:

<?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>";

The above will give you the print_r results you have. Therefore in order to access these properties you have to call on the getter functions that the class SHOULD have. as so:

foreach ($arr as $a) {
    echo $a->get_status();
    echo $a->get_number();
}

hope this helps.

CodeGodie
  • 12,116
  • 6
  • 37
  • 66