在PHP中将两个数组的项合并到另一个数组中

时间:2013-02-20 07:58:40

标签: php arrays

我下面有一个stdClass Object数组,但是我想将这些项合并到一个数组中,如下所示;

当前数组

Array
(
    [0] => stdClass Object
        (
            [photoid] => pht11a138355.jpg
            [propertyid] => PTY698082F7A
            [phototitle] => N/A
            [photodescription] => N/A
            [coverphoto] => 1
            [photovisible] => 1
        )

)

Array
(
    [0] => stdClass Object
        (
            [photoid] => phtd9d04dc92.jpg
            [propertyid] => PTY7419F7A38
            [phototitle] => N/A
            [photodescription] => N/A
            [coverphoto] => 1
            [photovisible] => 1
        )

)

我想要实现的目标;

Array
(
    [0] => stdClass Object
        (
            [photoid] => pht11a138355.jpg
            [propertyid] => PTY698082F7A
            [phototitle] => N/A
            [photodescription] => N/A
            [coverphoto] => 1
            [photovisible] => 1
        )
    [1] => stdClass Object
        (
            [photoid] => phtd9d04dc92.jpg
            [propertyid] => PTY7419F7A38
            [phototitle] => N/A
            [photodescription] => N/A
            [coverphoto] => 1
            [photovisible] => 1
        )
)

是否可以在PHP中执行

这是我正在使用的脚本的一部分;

foreach ($data['searchresults'] as &$row) {

$photo_array = $this->Properties_model->getimg($row->propertyid);

$photodata = $this->Properties_model->getimg($row->propertyid);

$data['photos'] = arrayToObject($photo_array);

echo '<pre>';
print_r($photo_array);
}

5 个答案:

答案 0 :(得分:1)

使用array_merge

$result = array_merge($array1, $array2);

答案 1 :(得分:1)

您正在寻找array_merge。

文档在这里: http://php.net/manual/en/function.array-merge.php

答案 2 :(得分:1)

试试这个:

<?php

$arr1 = Array(array(
            "photoid" => "pht11a138355.jpg",
            "propertyid" => "PTY698082F7A",
            "phototitle" => "N/A",
            "photodescription" => "N/A",
            "coverphoto" => 1,
            "photovisible" => 1
            )
);

$arr2 = Array(array(
            "photoid" => "phtd9d04dc92.jpg",
            "propertyid" => "PTY7419F7A38",
            "phototitle" => "N/A",
            "photodescription" => "N/A",
            "coverphoto" => 1,
            "photovisible" => 1
        )
);

$array  = array_merge($arr1,$arr2);

echo "<pre>";
print_r($array);
?>

答案 3 :(得分:0)

是..使用array_merge($array1,$array2)

答案 4 :(得分:0)

array_merge已经可用

<?php
$beginning = 'foo';
$end = array(1 => 'bar');
$result = array_merge((array)$beginning, (array)$end);
print_r($result);
?>

取自php文档。你应该先看过那里。

Array
    (
        [0] => foo
        [1] => bar
    )