简体   繁体   English

从多维数组中获取值

[英]Fetch values from multi dimensional array

I need to fetch data from the array that I got through 我需要从我通过的数组中获取数据

print_r($result);

The array I got is 我得到的数组是

Array
(
    [0] => Array
        (
            [id] => 
            [Location] => Array
                (
                    [img] => 177223
                    [name] => 
                )

            [Max] => 
            [Total] => 
            [Description] => Array
                (
                    [Pre] => 
                    [Updated] => 
                    [Program] => Array
                        (
                            [Schedule] =>
                        )
                )

            [Staff] => Array
                (
                    [FirstName] => 
                )
        )
)

I used this code 我用过这段代码

if (!empty($result)) 
    {
        foreach ($result as $res) 
            {
                $Max = $res['Max'];
                echo $Max;
                echo "<br>";
                    if(isset($res['Location']))
                        {
                            foreach($res['Location'] as $loc)
                                    {
                                        $img= $loc['img'];
                                        echo $img;
                                        echo "<br>";
                                    }   
                        }
            } 
    }       

I am getting correct value for the first array (Ie Max etc) but not for Location, Description and Staff, can anyone correct my code 我得到第一个数组(即最大等)的正确值,但不是位置,描述和工作人员,任何人都可以更正我的代码

Location is not an array of arrays. 位置不是数组数组。 It is just an associative array. 它只是一个关联数组。

if (!empty($result)) {
    foreach ($result as $res) {
            $Max = $res['Max'];
            echo $Max;
            echo "<br>";
            if(isset($res['Location'])) {
                 $img= $res['Location']['img'];
                 echo $img;
                 echo "<br>";
            }
     } 
}     

You dont need to foreach through location, just access it's elements directly: 你不需要通过位置来解决,只需直接访问它的元素:

if(isset($res['Location']))

     {
          $img= $res['Location']['img'];
          echo $img;
          echo "<br>";
     }   

Or somethig like that. 或者像那样的人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM