-4

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

当前阵列

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);
}
4

5 回答 5

1

使用array_merge.

$result = array_merge($array1, $array2);
于 2013-02-20T08:01:30.970 回答
1

您正在寻找 array_merge。

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

于 2013-02-20T08:01:54.713 回答
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);
?>
于 2013-02-20T08:32:13.447 回答
0

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

于 2013-02-20T08:01:55.793 回答
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
    )
于 2013-02-20T08:05:55.880 回答