从Array返回的键而不是Value

时间:2016-01-06 19:27:34

标签: php arrays foreach

我的数组有Mon - Sun,我只检索当前天数值并返回它以供JS使用。这就是数组的外观。

Array(
[Wednesday] => Array
        (
            [active] => Array
                (
                    [0] => yes
                )

            [start] => Array
                (
                    [0] => 13:30
                )

            [end] => Array
                (
                    [0] => 14:30
                )

        )

我还需要包括,阵列看起来因位置而异,因为开放时间不同。这是另一个位置的示例。

[Wednesday] => Array
        (
            [active] => Array
                (
                    [0] => yes
                    [1] => yes
                    [2] => yes
                )

            [start] => Array
                (
                    [0] => 13:30
                    [1] => 16:45
                    [2] => 09:30
                )

            [end] => Array
                (
                    [0] => 14:30
                    [1] => 17:00
                    [2] => 09:45
                )

        )

这是$ dayofweek

$dayofweek = date('l', strtotime($date));

这是我的foreach声明

foreach ($restarting as $data) {
    $close_now = $data[$dayofweek]['start'];
    $close_end = $data[$dayofweek]['end'];
    $booked_appt[] = $close_now;
    $booked_appt[] = $close_end;
}

以下是返回的内容

数组([0] => [1] => [2] => [3] => [4] => [5] => [6] => [7] => [8] => [9] => [10] => [11] => [12] => [13] =>)

我应该1:30 | 2:30

1 个答案:

答案 0 :(得分:1)

为什么你有“是”'和时代' 30'和' 30'在数组中的数组?它看起来不必要地复杂。

如果它应该是这样的话,你需要改变这个:

$close_now = $data[$dayofweek]['start'];

到这个

$close_now = $data[$dayofweek]['start'][0];

或者您可以简化$ data数组以包含更少的级别:

Array(

[星期三] =>排列         (             [有效] => '是&#39 ;,             [开始] => ' 13:30&#39 ;,             [end] => ' 14:30'         )