我在PHP中合并了几个像这样的数组
//all the other stuff
$array1 = (array) $new;
$array2 = (array) $info;
$ab = array('a' => $array1, 'b' => $array2);
print_r($ab);
print_r($ab)
给我这样的话:
Array
(
[a] => Array
(
[entries] => Array
(
[0] => stdClass Object
(
[title] => Serial - This American Life
[id] => Serial is a podcast from the creators of This American Life, hosted by Sarah Koenig.
)
[1] => stdClass Object
(
[title] => This American Life - This American Life
[id] => This American Life is a weekly public radio show, heard by 2.2 million people on more than 500 stations.
)
[2] => stdClass Object
(
[title] => Real Crime Profile - Real Crime Profile
[id] => Podcast talking about criminal cases and personality profiling.
)
)
)
[b] => Array
(
[entries] => Array
(
[0] => stdClass Object
(
[artistName] => This American Life
[feedUrl] => http://feeds.serialpodcast.org/serialpodcast
[primaryGenreName] => News & Politics
[artworkUrl60] => http://is2.mzstatic.com/image/thumb/Music69/v4/70/c9/71/70c97133-f3a8-738e-ea2c-27a6dc7d9731/source/60x60bb.jpg
)
[1] => stdClass Object
(
[artistName] => This American Life
[feedUrl] => http://feed.thisamericanlife.org/talpodcast
[primaryGenreName] => Personal Journals
[artworkUrl60] => http://is5.mzstatic.com/image/thumb/Music4/v4/1f/b8/0f/1fb80f69-bd94-8cad-0a2f-b082541d5f64/source/60x60bb.jpg
)
[2] => stdClass Object
(
[artistName] => Real Crime Profile
[feedUrl] => http://feeds.soundcloud.com/users/soundcloud:users:202076064/sounds.rss
[primaryGenreName] => History
[artworkUrl60] => http://is5.mzstatic.com/image/thumb/Music69/v4/e4/0d/1e/e40d1efe-f625-8d15-4e2e-706fecead1e8/source/60x60bb.jpg
)
)
)
)
这与我正在寻找的内容很接近,但理想情况下,我想更加简化这一点,因此我没有a
和b
"树"在这个数组中。
这就是我想要的结果:
Array
(
[a] => Array
(
[entries] => Array
(
[0] => stdClass Object
(
[title] => Serial - This American Life
[id] => Serial is a podcast from the creators of This American Life, hosted by Sarah Koenig.
[artistName] => This American Life
[feedUrl] => http://feeds.serialpodcast.org/serialpodcast
[primaryGenreName] => News & Politics
[artworkUrl60] => http://is2.mzstatic.com/image/thumb/Music69/v4/70/c9/71/70c97133-f3a8-738e-ea2c-27a6dc7d9731/source/60x60bb.jpg
)
[1] => stdClass Object
(
[title] => This American Life - This American Life
[id] => This American Life is a weekly public radio show, heard by 2.2 million people on more than 500 stations.
[artistName] => This American Life
[feedUrl] => http://feed.thisamericanlife.org/talpodcast
[primaryGenreName] => Personal Journals
[artworkUrl60] => http://is5.mzstatic.com/image/thumb/Music4/v4/1f/b8/0f/1fb80f69-bd94-8cad-0a2f-b082541d5f64/source/60x60bb.jpg
)
[2] => stdClass Object
(
[title] => Real Crime Profile - Real Crime Profile
[id] => Podcast talking about criminal cases and personality profiling.
[artistName] => Real Crime Profile
[feedUrl] => http://feeds.soundcloud.com/users/soundcloud:users:202076064/sounds.rss
[primaryGenreName] => History
[artworkUrl60] => http://is5.mzstatic.com/image/thumb/Music69/v4/e4/0d/1e/e40d1efe-f625-8d15-4e2e-706fecead1e8/source/60x60bb.jpg
)
)
)
)
我也试过了array_merge_recursive($array1['entries'],$array2['entries']
,但这给了我这样的东西(6个对象[2个来自一个数组,4个来自另一个]而不是3个):
Array
(
[0] => stdClass Object
(
[title] => Serial - This American Life
[id] => Serial is a podcast from the creators of This American Life, hosted by Sarah Koenig. Serial unfolds one story - a true story - over the course of a whole season.
)
[1] => stdClass Object
(
[title] => This American Life - This American Life
[id] => This American Life is a weekly public radio show, heard by 2.2 million people on more than 500 stations.
)
[2] => stdClass Object
(
[title] => Real Crime Profile - Real Crime Profile
[id] => Podcast talking about criminal cases and personality profiling.
)
[3] => stdClass Object
(
[artistName] => This American Life
[feedUrl] => http://feeds.serialpodcast.org/serialpodcast
[primaryGenreName] => News & Politics
[artworkUrl60] => http://is2.mzstatic.com/image/thumb/Music69/v4/70/c9/71/70c97133-f3a8-738e-ea2c-27a6dc7d9731/source/60x60bb.jpg
)
[4] => stdClass Object
(
[artistName] => This American Life
[feedUrl] => http://feed.thisamericanlife.org/talpodcast
[primaryGenreName] => Personal Journals
[artworkUrl60] => http://is5.mzstatic.com/image/thumb/Music4/v4/1f/b8/0f/1fb80f69-bd94-8cad-0a2f-b082541d5f64/source/60x60bb.jpg
)
[5] => stdClass Object
(
[artistName] => Real Crime Profile
[feedUrl] => http://feeds.soundcloud.com/users/soundcloud:users:202076064/sounds.rss
[primaryGenreName] => History
[artworkUrl60] => http://is5.mzstatic.com/image/thumb/Music69/v4/e4/0d/1e/e40d1efe-f625-8d15-4e2e-706fecead1e8/source/60x60bb.jpg
)
)
关于如何将这些合并为3个对象的想法?
答案 0 :(得分:1)
这只是一个示例,用于解释如何将my answer中的函数multiArrayCombine
用于另一个问题:
foreach( $array['a']['entries'] as &$val ) $val = (array) $val;
foreach( $array['b']['entries'] as &$val ) $val = (array) $val;
$result = multiArrayCombine( $array['a']['entries'], $array['b']['entries'], T_OBJECT_CAST );
$array['a']['entries'] = $result;
unset( $array['b'] );
print_r( $array );
的 eval.in demo 强>
正如我所说,这只是一个例子:该函数可以选择返回对象但不分析它。您可以修改它以处理对象并直接使用它,而不需要前两行。
答案 1 :(得分:0)
尝试
for($i=0;$i<count($array1['entries']); $i++){
$array3['entries'][$i] =(object) ((array) $array1['entries'][$i] + (array) $array2['entries'][$i]);
}
答案 2 :(得分:0)
函数array_replace_recursive
将执行您描述的操作。 array_merge_recursive
的问题在于,当遇到顺序数组时,它会连接合并数组的值,而不是用相同的索引覆盖这些条目。 array_replace_recursive
本质上是相同的,但始终将数组视为关联数,并且永远不会将它们连接起来。
http://php.net/manual/en/function.array-replace-recursive.php
由于您的叶子节点都是stdObject
并且array_merge_recursive
不能合并对象,因此您需要先将它们转换为数组。
示例:
<?php
$a = [
'entries' => [
(object) [
'title' => 'Alpha',
'id' => 'Alpha ...'
],
(object) [
'title' => 'Beta',
'id' => 'Beta ...'
]
]
];
$b = [
'entries' => [
(object) [
'artistName' => 'Alpha artist',
'feedUrl' => 'fdsfsdf'
],
(object) [
'artistName' => 'Beta artist',
'feedUrl' => 'gfdsgfds'
]
]
];
function convert_objects($array) {
array_walk_recursive($array, function(&$value) {
$value = (array) $value;
});
return $array;
}
var_export(array_replace_recursive(convert_objects($a), convert_objects($b)));
打印哪些:
array (
'entries' =>
array (
0 =>
array (
'title' => 'Alpha',
'id' => 'Alpha ...',
'artistName' => 'Alpha artist',
'feedUrl' => 'fdsfsdf',
),
1 =>
array (
'title' => 'Beta',
'id' => 'Beta ...',
'artistName' => 'Beta artist',
'feedUrl' => 'gfdsgfds',
),
),
)