简体   繁体   English

如何从PHP中的多维数组获取值?

[英]How get values from the multidimensional array in PHP?

I have an array like below. 我有一个像下面的数组。 I want to get the value of startTime and endTime. 我想获取startTime和endTime的值。 The values for the startTime and endTime should come in the dropdown. startTime和endTime的值应在下拉列表中。 Means the value of startTime and endTime for the 1st array block should come in one option of the select box and the 2nd array block should come in the 2nd option. 表示第一个数组块的startTime和endTime值应该位于选择框的一个选项中,而第二个数组块的值应位于第二个选项中。 I have around 100+ array block for this. 我有大约100个以上的数组块。

Array
(
    [upcoming] => Array
        (
            [webinars] => Array
                (
                    [0] => Array
                        (
                            [times] => Array
                                (
                                    [0] => Array
                                        (
                                            [startTime] => 2015-03-23T11:00:00Z
                                            [endTime] => 2015-03-23T12:00:00Z
                                        )

                                )
                        )

                    [1] => Array
                        (
                            [times] => Array
                                (
                                    [0] => Array
                                        (
                                            [startTime] => 2015-03-26T11:00:00Z
                                            [endTime] => 2015-03-26T12:00:00Z
                                        )

                                )

                        )
)

            [status] => 1
        )

)

So for getting the values from the array I have made my code like this 因此,为了从数组中获取值,我将代码编写如下

foreach($webinars as $key=>$val) {
    foreach($val as $test) {
      foreach($test as $ksh) {
        foreach($ksh['times'] as $k=>$v) {
          echo $v['startTime'];
          echo $v['endTime'];
        }
      }
    }
  }

But I don't think getting startTime and endTime by using 4 foreach is a smarterway. 但是我不认为通过使用4 foreach获得startTime和endTime是更聪明的方法。 I want to know is there any smarter way of getting those values? 我想知道有没有更聪明的方法来获得这些价值? Any help and suggestions will be really appreciable. 任何帮助和建议都是非常可取的。 Thanks 谢谢

the times and upcoming will have the one single array element always 时代和未来将永远只有一个数组元素

Well then the actual webinars level is the only thing that can have multiple entries, so something simple such as 那么实际的webinars级别是唯一可以有多个条目的东西,所以简单的事情例如

foreach($webinars['upcoming']['webinars'] as $webinar) {
  echo $webinar['times'][0]['startTime'] . ' - ' . $webinar['times'][0]['endTime'] . "<br>\n";
}

should do … (Assuming your variable that holds this whole array is named $webinars .) 应该做的……(假定将包含整个数组的变量命名为$webinars 。)

I don't know if this helps, or solve your problem, but maybe you can get an idea how to solve what you want in different way: 我不知道这是否对您有帮助,还是可以解决您的问题,但是也许您可以了解如何以不同的方式解决您想要的问题:

<?php
    $arr = array
    (
     'upcoming' => array
     (
      'webinars' => array
      (
       0 => array
       (
        'times' => array
        (
         0 => array
         (
          'startTime' => '2015-03-23T11:00:00Z',
          'endTime' => '2015-03-23T12:00:00Z'
          )

         )
        ),

       1 => array
       (
        'times' => array
        (
         0 => array
         (
          'startTime' => '2015-03-26T11:00:00Z',
          'endTime' => '2015-03-26T12:00:00Z'
          )

         )

        ),
       ),

      'status' => 1
      )

     );
    echo "<pre>";
    var_dump($arr);
    echo "</pre>";


    function searchByKey(array $array, $search,&$result) {
        foreach (new RecursiveIteratorIterator(new RecursiveArrayIterator($array)) as $key => $value) {
            if ($search === $key)
                $result[] = $value;
        }
        return false;
    }

    $startTimeArray = array();

    searchByKey($arr, 'startTime', $startTimeArray);

    var_dump($startTimeArray);

    $endTimeArray = array();

    searchByKey($arr, 'endTime', $endTimeArray);

    var_dump($endTimeArray);



?>

You only need array_map(), read more at: 您只需要array_map(),更多信息请参见:

http://php.net/manual/en/function.array-map.php http://php.net/manual/zh/function.array-map.php

Try this: 尝试这个:

$array = array ( 'upcoming' => array ( 'webinars' => array ( 0 => array ( 'times' => array ( 0 => array ( 'startTime' => '2015-03-23T11:00:00Z', 'endTime' => '2015-03-23T12:00:00Z', ), ), ), 1 => array ( 'times' => array ( 0 => array ( 'startTime' => '2015-03-26T11:00:00Z', 'endTime' => '2015-03-26T12:00:00Z', ), ), ), ), 'status' => '1', ), );

$start = array_map(function($n) {return $n['times'][0]['startTime'];}, $array['upcoming']['webinars']);

print_r($start);
echo "<br>";
/*
Array
(
    [0] => 2015-03-23T11:00:00Z
    [1] => 2015-03-26T11:00:00Z
)
*/

echo implode("<br>", $start);

/*
2015-03-23T11:00:00Z
2015-03-26T11:00:00Z
*/

$end = array_map(function($n) {return $n['times'][0]['endTime'];}, $array['upcoming']['webinars']);

echo "<br>";
print_r($end);    
echo "<br>";
/*
Array
(
    [0] => 2015-03-23T12:00:00Z
    [1] => 2015-03-26T12:00:00Z
)
*/

echo implode("<br>", $end);
/*
2015-03-23T12:00:00Z
2015-03-26T12:00:00Z
*/ 

Try this, this might help you: 试试这个,这可能对您有帮助:

<?php

    $array['upcoming']['webinars']=array(
            0 => array(
                    'times' => array(
                                0 => array(
                                        'startTime' => '2015-03-23T11:00:00Z',
                                        'endTime' => '2015-03-23T12:00:00Z',
                                    )
                        )

                ),
            1 => array(
                    'times' => array(
                                0 => array(
                                        'startTime' => '2015-03-26T11:00:00Z',
                                        'endTime' => '2015-03-26T12:00:00Z',
                                    )
                        )

                ),
        );


    ?>

    <select name="time_select">
    <option value = ''> Select Time </option>
    <?php

        $webinars = $array['upcoming']['webinars'];
        foreach($webinars as $key => $val) {
            foreach($val['times'] as $time) {
                echo '<option value="any_value">'.$time['startTime'].'-----'.$time['endTime'].'</option>';    
            }
      }
    ?>
    </select>

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

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