How can I merge two arrays together but keep the data of both arrays and sort of interlink the two arrays so the type of posts within the arrays alternate?
如何将两个数组合并在一起但保持两个数组的数据并将两个数组互连,以便数组中的帖子类型交替?
Take the following examples of data:
请参考以下数据示例:
$abc_posts = array(
array('id' => 1),
array('id' => 3)
);
$xyz_posts = array(
array('id' => 2),
array('id' => 4)
);
How can I merge them together so I can achieve this result?
如何将它们合并在一起以便我可以实现这一结果?
$merged_posts = array(
array('id' => 1),
array('id' => 2),
array('id' => 3),
array('id' => 4)
);
Notice that the arrays have been 'interlinked' and merged. I'm aware that array_merge
would work if I just wanted to merge the two together, but this overwrites arrays that are the same. I'd like to keep all of the data and not have one array overwrite another.
请注意,数组已经“互连”并合并。我知道如果我只想将两者合并在一起,array_merge会起作用,但这会覆盖相同的数组。我想保留所有数据而没有一个数组覆盖另一个数据。
It's also worth noting that the key, value pairs shown above is just an example to show that the arrays have in fact been merged in alternating order. I don't need to merge them in order of the id
value itself (if that makes sense?).
值得注意的是,上面显示的键值对只是一个示例,表明数组实际上已经以交替顺序合并。我不需要按照id值本身的顺序合并它们(如果这有意义吗?)。
5 个解决方案
#1
2
A combination of array_merge
and asort
should do the job based on the sample data.
array_merge和asort的组合应该根据示例数据完成工作。
$abc_posts = array(
array('id' => 1),
array('id' => 3)
);
$xyz_posts = array(
array('id' => 1),
array('id' => 2),
array('id' => 4)
);
$merge = array_merge($abc_posts,$xyz_posts);
asort($merge); // only needed if you want to sort on `id`
echo '<pre>';
var_dump($merge);
And the result:
结果如下:
array(5) {
[0]=> array(1) {
["id"]=> int(1)
}
[2]=> array(1) {
["id"]=> int(1)
}
[3]=> array(1) {
["id"]=> int(2)
}
[1]=> array(1) {
["id"]=> int(3)
}
[4]=> array(1) {
["id"]=> int(4)
}
}
NOTE: You talk of an issue with array_merge
but I can not see or replicate any issue unless the original array key is important.
注意:您谈到array_merge的问题,但除非原始数组键很重要,否则我无法查看或复制任何问题。
#2
1
You can just use the classic foreach
loop.
您可以使用经典的foreach循环。
$abc_posts = array(
array('id' => 1),
array('id' => 3)
);
$xyz_posts = array(
array('id' => 2),
array('id' => 4)
);
foreach( $abc_posts as $key => $val ) {
$merged_posts[] = $val; //Push the current abc_posts element
$merged_posts[] = $xyz_posts[$key]; //Push the xyz_posts with the same key/index
}
echo "<pre>";
print_r( $merged_posts );
echo "</pre>";
This will result to:
这将导致:
Array
(
[0] => Array
(
[id] => 1
)
[1] => Array
(
[id] => 2
)
[2] => Array
(
[id] => 3
)
[3] => Array
(
[id] => 4
)
)
#3
0
As per the comments and question edit, this function interleaves the two arrays, taking one element from A, one from B, etc. But the example data was indeed misleading, which is why the other answers assume a sort by keys.
根据评论和问题编辑,这个函数交错两个数组,从A中取一个元素,从B中取一个等等。但是示例数据确实具有误导性,这就是为什么其他答案假设按键排序的原因。
function array_interleave($a, $b) {
$ret = array();
$posA = 0;
$posB = 0;
$i = 0;
while ($posA < count($a) && $posB < count($b)) {
$ret[] = ($i++ % 2 == 0 ? $a[$posA++] : $b[$posB++]);
}
while ($posA < count($a) || $posB < count($b)) {
$ret[] = ($posA < count($a) ? $a[$posA++] : $b[$posB++]);
}
return $ret;
}
在线尝试!
#4
0
Try this
$c = array_merge($a,$b);
asort($c);
echo "<pre>";print_r($a);
or
foreach($a as $aa){
$b[] = $aa;
}
asort($b);
echo "<pre>";print_r($b);
#5
0
$abc_posts = array(
array('id' => 1),
array('id' => 3)
);
$xyz_posts = array(
array('id' => 1),
array('id' => 2),
array('id' => 4)
);
$arr_merge = array_merge($abc_posts,$xyz_posts);
asort($arr_merge);
echo "<pre>"; print_r($arr_merge); echo "</pre>";
For More Example : http://php.net/manual/en/function.array-merge.php
更多示例:http://php.net/manual/en/function.array-merge.php
#1
2
A combination of array_merge
and asort
should do the job based on the sample data.
array_merge和asort的组合应该根据示例数据完成工作。
$abc_posts = array(
array('id' => 1),
array('id' => 3)
);
$xyz_posts = array(
array('id' => 1),
array('id' => 2),
array('id' => 4)
);
$merge = array_merge($abc_posts,$xyz_posts);
asort($merge); // only needed if you want to sort on `id`
echo '<pre>';
var_dump($merge);
And the result:
结果如下:
array(5) {
[0]=> array(1) {
["id"]=> int(1)
}
[2]=> array(1) {
["id"]=> int(1)
}
[3]=> array(1) {
["id"]=> int(2)
}
[1]=> array(1) {
["id"]=> int(3)
}
[4]=> array(1) {
["id"]=> int(4)
}
}
NOTE: You talk of an issue with array_merge
but I can not see or replicate any issue unless the original array key is important.
注意:您谈到array_merge的问题,但除非原始数组键很重要,否则我无法查看或复制任何问题。
#2
1
You can just use the classic foreach
loop.
您可以使用经典的foreach循环。
$abc_posts = array(
array('id' => 1),
array('id' => 3)
);
$xyz_posts = array(
array('id' => 2),
array('id' => 4)
);
foreach( $abc_posts as $key => $val ) {
$merged_posts[] = $val; //Push the current abc_posts element
$merged_posts[] = $xyz_posts[$key]; //Push the xyz_posts with the same key/index
}
echo "<pre>";
print_r( $merged_posts );
echo "</pre>";
This will result to:
这将导致:
Array
(
[0] => Array
(
[id] => 1
)
[1] => Array
(
[id] => 2
)
[2] => Array
(
[id] => 3
)
[3] => Array
(
[id] => 4
)
)
#3
0
As per the comments and question edit, this function interleaves the two arrays, taking one element from A, one from B, etc. But the example data was indeed misleading, which is why the other answers assume a sort by keys.
根据评论和问题编辑,这个函数交错两个数组,从A中取一个元素,从B中取一个等等。但是示例数据确实具有误导性,这就是为什么其他答案假设按键排序的原因。
function array_interleave($a, $b) {
$ret = array();
$posA = 0;
$posB = 0;
$i = 0;
while ($posA < count($a) && $posB < count($b)) {
$ret[] = ($i++ % 2 == 0 ? $a[$posA++] : $b[$posB++]);
}
while ($posA < count($a) || $posB < count($b)) {
$ret[] = ($posA < count($a) ? $a[$posA++] : $b[$posB++]);
}
return $ret;
}
在线尝试!
#4
0
Try this
$c = array_merge($a,$b);
asort($c);
echo "<pre>";print_r($a);
or
foreach($a as $aa){
$b[] = $aa;
}
asort($b);
echo "<pre>";print_r($b);
#5
0
$abc_posts = array(
array('id' => 1),
array('id' => 3)
);
$xyz_posts = array(
array('id' => 1),
array('id' => 2),
array('id' => 4)
);
$arr_merge = array_merge($abc_posts,$xyz_posts);
asort($arr_merge);
echo "<pre>"; print_r($arr_merge); echo "</pre>";
For More Example : http://php.net/manual/en/function.array-merge.php
更多示例:http://php.net/manual/en/function.array-merge.php