PHP foreach多数组

时间:2014-01-06 11:53:52

标签: php arrays wordpress multidimensional-array foreach

我使用https://github.com/humanmade/Custom-Meta-Boxes/将此自定义可重复自定义字段设置为wordpress。给定的数组就像这样

    Array
(
    [0] => Array
        (
            [photoset-caption] => Array
                (
                    [cmb-field-0] => Caption1
                    [cmb-field-1] => Caption2
                    [cmb-field-2] => Caption3
                    [cmb-field-3] => Caption4
                )

            [photoset-image] => Array
                (
                    [cmb-field-0] => 17
                    [cmb-field-1] => 16
                    [cmb-field-2] => 15
                    [cmb-field-3] => 14
                )

        )

    [1] => Array
        (
            [photoset-caption] => Array
                (
                    [cmb-field-0] => Caption1
                    [cmb-field-1] => Caption2
                    [cmb-field-2] => Caption3
                    [cmb-field-3] => Caption4
                )

            [photoset-image] => Array
                (
                    [cmb-field-0] => 17
                    [cmb-field-1] => 16
                    [cmb-field-2] => 15
                    [cmb-field-3] => 14
                )

        )

)

它试图制作的循环就是这个。

 // get the custom fields for this post
    $photoset = get_post_meta($post->ID, 'photoset_group_fields', false ); 

        echo '<div class="photoset">';
        echo '<div class="photoset-row">';

        foreach($photoset as $photosetloop){
        echo '<figure class="photoset-item">';
        echo '<div>' . wp_get_attachment_image($photosetloop['photoset-image'], 'large' ) . '</div>';
        echo '<figcaption>' . $photosetloop['photoset-caption'] .'</figcaption>';
        echo '</figure>';
        }   

        echo '</div>';
        echo '</div>';

所以循环有.photoset-item,里面有图像和标题。 我的问题如何预测它,谢谢。

我确实更新了数组,我有一个循环的组。

2 个答案:

答案 0 :(得分:3)

制作两个foreach的最简单方法是,如果你想要的话,可以用函数递归。

<?php
$photos =array(
    'photoset-caption' => array(
            'cmb-field-0' => 'Caption1',
            'cmb-field-1' => 'Caption2',
            'cmb-field-2' => 'Caption3',
            'cmb-field-3' => 'Caption4'
     ),
    'photoset-image' => array(
            'cmb-field-0' => 17,
            'cmb-field-1' => 16,
            'cmb-field-2' => 15,
            'cmb-field-3' => 14
    )
);

    foreach($photos as $k => $photo){

        echo '<h1>'.$k.'</h1>';

        foreach($photo as $key => $value){
            echo $key.': '. $value.'<br/>';
        }
        echo '<hr/>';
    }

<?php $photos =array( 'photoset-caption' => array( 'cmb-field-0' => 'Caption1', 'cmb-field-1' => 'Caption2', 'cmb-field-2' => 'Caption3', 'cmb-field-3' => 'Caption4' ), 'photoset-image' => array( 'cmb-field-0' => 17, 'cmb-field-1' => 16, 'cmb-field-2' => 15, 'cmb-field-3' => 14 ) ); foreach($photos as $k => $photo){ echo '<h1>'.$k.'</h1>'; foreach($photo as $key => $value){ echo $key.': '. $value.'<br/>'; } echo '<hr/>'; }

点击此处查看示例:example

答案 1 :(得分:1)

这不是最优雅的方式,但在这种情况下确实有效。

$arr = Array
(
    "photoset-caption" => Array
    (
        "cmb-field-0" => "Caption1",
        "cmb-field-1" => "Caption2",
        "cmb-field-2" => "Caption3",
        "cmb-field-3" => "Caption4"
    ),

"photoset-image" => Array
    (
        "cmb-field-0" => 17,
        "cmb-field-1" => 16,
        "cmb-field-2" => 15,
        "cmb-field-3" => 14
    )
);

$count = count($arr["photoset-caption"]);
for($i = 0; $i < $count; ++$i) {
    echo 'Loop #'.$i.'<br>';
    echo $arr["photoset-caption"]["cmb-field-".$i], '<br>';
    echo $arr["photoset-image"]["cmb-field-".$i], '<br>';
    echo '<br>';
}

您可以将其粘贴在此处: http://writecodeonline.com/php/