How to manipulate an array with another array?
Hi I have a problem to manipulate an array with another array like the sample seen below.
Sample scenario
Array one contains elements A, B, C, D, E ordering is strict
B's position is constant, it will not change in any manipulation
Array two contain X, Y
I need to merge array two with array one, merging result array will be:
Result array: X, B, Y, A, C D E
- A changed its position with X, because A's position isnot constant
- B's positions cannot changed, because B's position is constant
- C changed its position with Y, because C's position isnot constant
- A and C is appended after changes
Note: More than one element's positiob can be constant
What do you suggest for this kind of problem?
tHanks
Edit:
Thanks to @mkilmanas, I changed his code it works in the scenario described above
$arrayOne = array(
0 => array('val' =>开发者_Go百科; 'A','is_const' => 0),
1 => array('val' => 'B','is_const' => 1),
2 => array('val' => 'C','is_const' => 0),
3 => array('val' => 'D','is_const' => 0),
4 => array('val' => 'E','is_const' => 0)
);
$arrayTwo = array(
0 => array('val' => 'X','is_const' => 0),
1 => array('val' => 'Y','is_const' => 0)
);
//Clone arrayOne
$loose = $arrayOne;
//Collect Fixed Elements
$fixed = array();
foreach ($arrayOne as $idx=>$val){
if ($val['is_const'] == 1){
$fixed[] = $idx;
}
}
//Now remove fixed elements
//PHP doesn't reindex the array, so it is safe to use foreach here
foreach($fixed as $idx) { unset($loose[$idx]); }
//since they are numeric-indexed, they will be concatenated
//and the order is inverted, so that $arrayOne is appended to the end of $arrayTwo
$combined = array_merge($arrayTwo, $loose);
// And now we insert the fixed elements at their fixed positions
foreach($fixed as $idx) {
array_splice($combined, $idx, 0, array($arrayOne[$idx]));
}
Result of $combined is X B Y A C D E which is correct
tHank you very much
I would use a combination of array_merge()
and array_splice()
. Lets assume that $fixed
contains an array of fixed element's keys (from $arrayOne
).
$loose = $arrayOne; // Copy initial array
// Now remove fixed elements
// PHP doesn't reindex the array, so it is safe to use foreach here
foreach($fixed as $idx) { unset($loose[$idx]); }
// since they are numeric-indexed, they will be concatenated
// and the order is inverted, so that $arrayOne is appended to the end of $arrayTwo
$combined = array_merge($arrayTwo, $arrayOne);
// And now we insert the fixed elements at their fixed positions
foreach($fixed as $idx) {
array_splice($combined, $idx, 0, array($arrayOne[$idx]));
}
// voila - $combined should now have the desired order
You could have a second array of boolean value of the same length as the first array. Each element in the boolean array would tell whether the corresponding element in the first array is moveable or not. Then, you could shift things appropriately.
You might want to start by building a multi dimensional array with slots/ranks for each level, so like:
$multi[10][] = $x;
$multi[10][] = $b;
$multi[20][] = $y;
$multi[20][] = $a;
Once that is done, you can go about flattening the array whilst preserving the order.
精彩评论