2

Let's say that I have two arrays like this:

$arr1 = array(
array('position' => 1),
array('position' => 2),
array('position' => 3),
array('position' => 4),
array('position' => 5),
array('position' => 6),
array('position' => 7)
);

$arr2 = array(      
array(
    'a' => 'A1', 
    'b' => 'B1',
    'c' => 'C1'
),
array(
    'a' => 'A2',
    'b' => 'B2',
    'c' => 'C2'
),
array(
    'a' => 'A3',
    'b' => 'B3',
    'c' => 'C3'
)
);

The goal is to have a resulting array where the key position from the first array is copied to each array in the second array like this:

$final_arr = array(
array(
    'a' => 'A1',
    'b' => 'B1',
    'c' => 'C1', 
    'position' => 1
),
array(
    'a' => 'A2',
    'b' => 'B2',
    'c' => 'C2', 
    'position' => 2
),
array(
    'a' => 'A3',
    'b' => 'B3',
    'c' => 'C3', 
    'position' => 3
)
);

Why can't I just do this with array_merge? Any idea?

NOTE As you can see above, the arrays don't have the same length

Thank you for any help

3
  • 1
    foreach() loop through array2 Commented Mar 10, 2014 at 1:11
  • As @Dagon suggests, iteration is the most obvious method. Commented Mar 10, 2014 at 1:11
  • What do you want to happen to $arr1 > 3? Commented Mar 10, 2014 at 1:16

2 Answers 2

2
$new=array();
foreach ($arr2 as $k=>$v){

$new[]=$v+$arr1[$k];
}

print_r($new);

http://codepad.viper-7.com/tYkEWa

Sign up to request clarification or add additional context in comments.

3 Comments

Can I do this with while loop?
excuse me , has been bothering you all. I have the same problem like this. I want ask when my arr2 loger then arr1 how to give a value 0 to the arr2 position = 0 ?
@Stfvns if you have a question, post a new question.
0

array_merge would just join the arrays. You'd get an array made up of the values (subarrays) of $arr1 and $arr2. (array_merge actually has two different behaviors, depending on whether the arrays it operates on have numeric keys or string/associative keys.) What you want is:

foreach($arr1 as $idx => $subar) {
  if (!isset($arr2[$idx])) {
    break;
  }
  $arr2[$idx]['position'] = $subar['position'];
}

I'm assuming the 'position' key is fixed. If it's not, then you have to guarantee that each $subar has a single and known key to extract using something like array_shift(array_keys($subar)).

4 Comments

your finial output does not match user765368's as you loop arr1 not arr2 (codepad.viper-7.com/TrsvAq)
@Dagon I copy the subarray from array 1 to the 'position' key in array 2. Since OP asked for array_merge, I felt updating an existing array would be more aligned with that request.
yes, but his finial array has 3 elements, not the 7 you return .. all good, up to the OP ;-)
The question seems a bit ambiguous to me. The OP is asking for a "merge" and then shows an array only containing 3 elements.

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.