How to access an array of arrays in PHP

后端 未结 3 513
不知归路
不知归路 2021-01-29 08:21

I am working on Flight api. I am sending the request(From location,To location,From date..etc) and i am getting the response in the array format as below.

Array
         


        
相关标签:
3条回答
  • 2021-01-29 08:51

    You will need to loop inside your array, level by level, and peint all relevant data you need. You can use foreach function to achieve this, please check documentation.

    0 讨论(0)
  • 2021-01-29 08:55

    Ok First let me show you how to do this using a simple example

        $myArray = array();
        $myArray[] = array('name'=>'Russell');
        $myArray[] = array('name'=>'Sam')
        foreach($myArray as $list)
        {
        print $list['name'];
        }
    

    you can see what this example shows is it will loop through each array and get the name.

    Now lets do a sub array in an array

        $myArray = array();
        $myArray[] = array('name'=>'Russell', 'about_me'=>array('age'=>22));
        $myArray[] = array('name'=>'Sam', 'about_me'=>array('age'=>32))
        foreach($myArray as $list)
        {
        print $list['name'];
        }
    
        foreach($myArray as $list)
        {
        print $list['name'];
    print $list['about_me']['age']; <-- this will print about the age of the person.
        }
    
    0 讨论(0)
  • 2021-01-29 09:07

    First print your array:

    print_r($yourarrayname);
    

    Then print:

    print_r($yourarrayname['AvailRequest']);
    

    Then print:

    print_r($yourarrayname['AvailRequest']['Trip']);
    

    Then you will understand what I mean.

    0 讨论(0)
提交回复
热议问题