2

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' =>  '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

4
  • Can both arrays contain elements with constant position or only one of them? Commented May 17, 2011 at 19:33
  • How do you determine that an Item is constant? Also, what do you need it for? (Maybe there already is a way). Commented May 17, 2011 at 19:34
  • @CrisDeBlonde Only first array can contain one or more constant positioned elements Commented May 18, 2011 at 10:21
  • @Lukas Knuth In fact problem is more complex, and it's very difficult to describe it here without showing the whole code base. I added a sample solution above with the help of @mkilmanas. Commented May 18, 2011 at 10:22

3 Answers 3

1

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
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your answer, I put the completed solution to the above.
0

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.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.