我想组合2个数组,但我希望元素的顺序基于存储在另一个数组中的索引值。
$old = array("aaa", "ccc", "ddd");
$oldPos = array(0, 2, 3);
$new = array("bbb", "eee");
$combinedArr = //This is where I need help combining the $old and $new based on the logic explained below.
$oldPos
包含与$old
数组中每个元素对应的索引号。所以在这个例子中,“aaa”将在新数组的索引0中,“ccc”将在索引2中,“ddd”将在索引3中。
$ combinedArr应打印出来:
"aaa", "bbb", "ccc", "ddd", "eee"
答案 0 :(得分:2)
您可以使用array_combine
。对于值,只需将两个数组与array_merge
粘合在一起即可。对于键,首先,使用range
获取整个结果数组大小,然后删除所采用的键(来自
$oldPos
}与array_diff
。并再次粘合$oldPos
并产生差异。那阵列将是你的位置。最后,您可能希望使用ksort
$old = array("aaa", "ccc", "ddd");
$oldPos = array(0, 2, 3);
$new = array("bbb", "eee");
$combinedArr = array_combine(
array_merge(
$oldPos,
// Deduce keys for new array elements substacting $oldPos
// from the whole range
array_diff(range(0, count($old) + count($new) -1), $oldPos)
),
array_merge($old, $new)
);
ksort($combinedArr);
这是working demo。
这种方法可以避免使用显式循环(除非你是微优化人员,这是一件好事)并确保你从标准PHP库中获取尽可能多的东西。
答案 1 :(得分:1)
这依赖于所有数组都正确排序(尽管我认为这是你尝试做的事情的极限)
<?php
$old = ["aaa", "ccc", "ddd"];
$oldPos = [0, 2, 3];
$new = ["bbb", "eee"];
$combinedArr = [];
$i = 0;
// Keep looping until we've exhausted both arrays
while (count($old) + count($new) > 0) {
// If $i matches an entry in $oldPos, grab the entry from $old, otherwise from $new
$combinedArr[] = in_array($i, $oldPos) ? array_shift($old) : array_shift($new);
$i++;
}
print_r($combinedArr); // ["aaa", "bbb", "ccc", "ddd", "eee"]
答案 2 :(得分:1)
试试这个:
$old = array("aaa", "ccc", "ddd");
$oldPos = array(0, 2, 3);
$new = array("bbb", "eee");
$oldKeys = array_combine($oldPos, $old);
foreach ($new as $key => $val) {
$curKey = $key;
while (isset($oldKeys[$curKey])) {
$curKey++;
}
$oldKeys[$curKey] = $val;
}
sort($oldKeys);
var_dump($oldKeys);
答案 3 :(得分:1)
希望这个会有所帮助。
<?php
ini_set('display_errors', 1);
$old = array("aaa", "ccc", "ddd");
$oldPos = array(0, 2, 3);
$new = array("bbb", "eee");
$count=count($old)+count($new); // count of the no of elements of both the array.
$arrayFlip= array_flip($oldPos);//flipping the array over values
$combined=array();
for($x=0;$x<$count;$x++)
{
if(isset($arrayFlip[$x]))
{
$combined[$x]= current($old);//using current element of array
unset($old[key($old)]);//unsetting current element of array
}
else
{
$combined[$x]= current($new);//using current element of array
unset($new[key($new)]);//unsetting current element of array
}
}
print_r($combined);
答案 4 :(得分:1)
握住手机,我发现使用array_multisort()
更直接的方法,这意味着您不必将合并的密钥拼接到合并的值。与使用sevavietl的答案评论的7函数方法相比,这使用了6个函数调用。
我更改了old
和new
数组中的值,以区分输出数组不按字母顺序排序(OP的示例数据可能会对未来的读者造成一些混淆)。
演示#1:详细样式
$oldvals = array("Last", "Fourth", "Second");
$oldkeys = array(4, 3, 1);
$newvals = array("First", "Third");
$vals=array_merge($oldvals,$newvals); // forge one array with old elements first and new elements second
$newkeys=array_diff(range(0,sizeof($vals)-1),$oldkeys); // generate all necessary keys, omit "old" keys
$keys=array_merge($oldkeys,$newkeys); // forge one array with old keys first and new keys second (as values)
array_multisort($keys,$vals); // sort unordered keys and unordered vals in the same fashion
var_export($vals);
演示#2:浓缩风格
$oldvals=['First','Third','Fourth'];
$oldkeys=[0,2,3];
$newvals=['Second','Last'];
$vals=array_merge($oldvals,$newvals);
array_multisort(array_merge($oldkeys,array_diff(range(0,sizeof($vals)-1),$oldkeys)),$vals);
var_export($vals);
来自任一代码块的输出:
array (
0 => 'First',
1 => 'Second',
2 => 'Third',
3 => 'Fourth',
4 => 'Last',
)
这是一个循环for
循环方法,不会破坏无序$oldkeys
值。 Demo with test of OP's data, and unordered data
$oldvals = array("Last", "Fourth", "Second");
$oldkeys = array(4, 3, 1);
$newvals = array("First", "Third");
for($i=0, $count=sizeof($oldvals)+sizeof($newvals); $i<$count; ++$i){
$result[$i]=(($k=array_search($i,$oldkeys))!==false?$oldvals[$k]:array_shift($newvals));
}
var_export($result); // same result as my earlier method