How to access an array of arrays in PHP

后端 未结 3 518
不知归路
不知归路 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: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.
        }
    

提交回复
热议问题