二维PHP数组输出不正确


Two dimensional PHP array not outputting correctly

我有下面的日期和地点数组-基本上每个日期都需要允许多个地点。我正在尝试将下面的数组显示为以下格式:

20140411
贝辛斯托克
索尔兹伯里

20140405
贝辛斯托克

20140419
索尔兹伯里

等等

阵列:

Array
(
    [20140411] => Array
        (
            [0] => Array
                (
                    [0] => Basingstoke
                )
            [1] => Array
                (
                    [0] => Salisbury
                )
        )
    [20140405] => Array
        (
            [0] => Array
                (
                    [0] => Basingstoke
                )
        )
    [20140419] => Array
        (
            [0] => Array
                (
                    [0] => Salisbury
                )
        )
    [20140427] => Array
        (
            [0] => Array
                (
                    [0] => Basingstoke
                )
        )
)

我相信我已经接近了,但在处理数组/键等时,我总是有一些心理障碍。我正在尝试做一个嵌套的foreach循环,它可以很好地显示日期,但只是为位置输出"数组":

foreach ($dates as $date => $dateKey) {
    // Format the date
    $theDate = DateTime::createFromFormat('Ymd', $date);
    $theFormattedDate = $theDate->format('d-m-Y');
    echo '<h4>'.$theFormattedDate.'</h4>';
    foreach ($dateKey as $key => $venue) {
        echo $venue;
    }
}

有人能发现我哪里出了问题吗?

编辑:

以下是创建阵列的位置,如果有帮助的话?

$dates = array();
while ( have_rows('course_date') ) : the_row(); 
    $theVenue = get_sub_field('venue');
    // Use the date as key to ensure values are unique
    $dates[get_sub_field('date')][] = array(
        $theVenue->post_title
    );
endwhile; 

在您的案例中,场所是一个数组
它始终是一个数组,其中只有一个元素可以寻址为[0]
因此

foreach ($dates as $date => $dateKey) {
    // Format the date
    $theDate = DateTime::createFromFormat('Ymd', $date);
    $theFormattedDate = $theDate->format('d-m-Y');
    echo '<h4>'.$theFormattedDate.'</h4>';
    foreach ($dateKey as $key => $venue) {
        echo $venue[0];
    }
}

或者,如果您可以在最后一级数组中有多个场地,您可以重写内部foreach并添加另一个:

foreach ($dates as $date => $dateKey) {
    // Format the date
    $theDate = DateTime::createFromFormat('Ymd', $date);
    $theFormattedDate = $theDate->format('d-m-Y');
    echo '<h4>'.$theFormattedDate.'</h4>';
    foreach ($dateKey as $key => $venues) {
        foreach($venues as $v) {
           echo $v;
        }
    }
}

位置嵌套更深一层,您需要一个foreach

没关系,另一个人说这个插件应该是这样工作的:)