如何组合几个多维数组?

时间:2013-10-02 23:03:10

标签: php arrays multidimensional-array

每个新传递都有一个do-while循环创建一个新数组。


do-while 循环:

$i = 0;
do {
    //  Create an array of data from the response
        $array_data = array();

        foreach ($response['data'] as $data) {
            $created_time = $data['created_time'];              
            $thumbnail = $data['images']['thumbnail']['url'];
            $tags_name = $get_TagIMG;

            $array_data[] = array
            (
                'DATE_PIC' => $created_time,
                'LINK_PIC' => $thumbnail,
                'TAG_PIC' => $tags_name,
            );
        }   
    $i++;

    } while ($i < 5);

在循环结束后,我们得到五个数组,如下所示:

Array (
    [0] => Array ( [DATE_PIC] => 1380488912 [LINK_PIC] => http://distilleryimage0.s3.amazonaws.com/4c39144e294b11e3bef022000a1f9245_5.jpg [TAG_PIC] => pricelessmoscow )
)

Array (
    [1] => Array ( [DATE_PIC] => 1380489222 [LINK_PIC] => http://distilleryimage5.s3.amazonaws.com/04ca9032294c11e3b53822000a1f96e6_5.jpg [TAG_PIC] => pricelessmoscow )
)

Array (
    [2] => Array ( [DATE_PIC] => 1380492460 [LINK_PIC] => http://distilleryimage9.s3.amazonaws.com/8e755dd8295311e3a53722000aeb43de_5.jpg [TAG_PIC] => pricelessmoscow )
)

Array (
    [3] => Array ( [DATE_PIC] => 1380718591 [LINK_PIC] => http://distilleryimage5.s3.amazonaws.com/0f8bcf142b6211e3982822000ae90f30_5.jpg [TAG_PIC] => pricelessmoscow )
)

Array (
    [4] => Array ( [DATE_PIC] => 1380717412 [LINK_PIC] => http://distilleryimage7.s3.amazonaws.com/507c69fa2b5f11e3a7d422000a9e0846_5.jpg [TAG_PIC] => pricelessmoscow )
)

问题:如何将这些数组合并为一个?


结果应该是这样的:

Array (
    [0] => Array ( [DATE_PIC] => 1380488912 [LINK_PIC] => http://distilleryimage0.s3.amazonaws.com/4c39144e294b11e3bef022000a1f9245_5.jpg [TAG_PIC] => pricelessmoscow )
    [1] => Array ( [DATE_PIC] => 1380489222 [LINK_PIC] => http://distilleryimage5.s3.amazonaws.com/04ca9032294c11e3b53822000a1f96e6_5.jpg [TAG_PIC] => pricelessmoscow )
    [2] => Array ( [DATE_PIC] => 1380492460 [LINK_PIC] => http://distilleryimage9.s3.amazonaws.com/8e755dd8295311e3a53722000aeb43de_5.jpg [TAG_PIC] => pricelessmoscow )
    [3] => Array ( [DATE_PIC] => 1380718591 [LINK_PIC] => http://distilleryimage5.s3.amazonaws.com/0f8bcf142b6211e3982822000ae90f30_5.jpg [TAG_PIC] => pricelessmoscow )
    [4] => Array ( [DATE_PIC] => 1380717412 [LINK_PIC] => http://distilleryimage7.s3.amazonaws.com/507c69fa2b5f11e3a7d422000a9e0846_5.jpg [TAG_PIC] => pricelessmoscow )
)

1 个答案:

答案 0 :(得分:0)

您的源代码似乎是正确的,这正是您正在运行的吗?

而且,如果您想要查看所有对象,而不仅仅是N,则可以使用foreach()

<?php

    // used to populate
    $sourceData = array(1,2,3);

    // array of arrays
    $data = array();

    foreach( $sourceData as $val )
    {
        $data[] = array( "date" => "date_" . $val,
                         "link" => "link_" . $val,
                         "tag"  => "tag_"  . $val );
    }

    var_dump($data);    

?>

输出:

array(3) {
  [0]=>
  array(3) {
    ["date"]=>
    string(6) "date_1"
    ["link"]=>
    string(6) "link_1"
    ["tag"]=>
    string(5) "tag_1"
  }
  [1]=>
  array(3) {
    ["date"]=>
    string(6) "date_2"
    ["link"]=>
    string(6) "link_2"
    ["tag"]=>
    string(5) "tag_2"
  }
  [2]=>
  array(3) {
    ["date"]=>
    string(6) "date_3"
    ["link"]=>
    string(6) "link_3"
    ["tag"]=>
    string(5) "tag_3"
  }
}