0

I need your help with my problem. My problem is I have 2 arrays the first one is the main array. The second is the array for my new data.

Let's say I have these arrays.

This is the main array:

Array
(
    0 => Array
        (
            'id' => 1,
            'name' => 'Apple',
            'age' => 12
         )

    1 => Array
        (
            'id' => 2,
            'name' => May,
            'age' => 13
         )
)

This is the second array:

Array
(
    1 => Array
        (
            'gender' => 'Male'
        )

    2 => Array
        (
            'gender' => 'Female'
        )

)

And I have this loop in PHP

foreach($main_array as &$main){

   //this is the loop inside the first array
   // how can I add the second array with it?

}

This is the sample output:

[0] => Array
    (
        [id] => 1
        [name] => Apple
        [age] => 12
        [gender] => Female
     )

[1] => Array
    (
        [id] => 2
        [name] => May
        [age] => 13
        [gender] => Female
     )

How can I do that? Any suggestions? That's all thanks.

5
  • What you mean by "add the second array with it"?
    – hindmost
    Feb 13, 2014 at 8:12
  • 1
    Is the second array keys are id of the first array ?
    – Rikesh
    Feb 13, 2014 at 8:13
  • I mean I just want to add the gender in my first array
    – Jerielle
    Feb 13, 2014 at 8:14
  • 1
    All you need is standard function array_merge_recursive() (see php.net/manual/en/function.array-merge-recursive.php )
    – Ilya
    Feb 13, 2014 at 8:16
  • @Ilya array_merge_recursive doesn't merge values for numeric keys (as in given arrays)
    – hindmost
    Feb 13, 2014 at 8:22

5 Answers 5

3
for($i=0; $i<count($main_array); $i++){
    for($j=0; $j<count($second_array); $j++){
        if($main_array[$i]['id'] == $j){
            $main_array[$i]['gender'] = $second_array[$j]['gender']
        }
    }
}
1

I fixed your example code, it wont run otherwise.

<?php

// Test data:
$main_array = Array(
  0 => Array(
    'id' => 1,
    'name' => 'Apple',
    'age' => 12
  ),
  1 => Array (
    'id' => 2,
    'name' => 'May',
    'age' => 13
  )
);
$lookup = Array(
  1 => Array(
    'gender' => 'Male'
  ),
  2 => Array(
    'gender' => 'Female'
  )
);

// Your answer:
foreach ($main_array as &$main) {
  if (array_key_exists($main['id'],$lookup)) {
    $main['gender'] = $lookup[$main['id']]['gender'];  // <-- sets gender value
  }
}

// Output it to browser:
echo '<pre>$main_array = '.print_r($main_array,true).'</pre>';

The array_key_exists() check is there to avoid errors such as PHP Notice: Undefined offset: 123 when the $lookup data is incomplete.

0
1

If you want to merge all of the data from both arrays:

PHP tools:

The exact behaviors of these functions needs to be studied and tested before usage, to make sure it fits your intent.

//  array merge recursive doesn't merge numeric keys
$main_array = array_merge_recursive($main_array, $secondary_array);

//  array replace recursive has a downside of replacing stuff
$main_array = array_replace_recursive($main_array, $secondary_array);

Rolling your own:

foreach($main_array as $i => &$main){
    if(isset($secondary_array[$i])) {
        foreach($secondary_array[$i] AS $key => $value) {
            $main[$key] = $value;
        }
    }
}

Both of the above solutions only apply if the array-indexes of $main_array and $secondary_array match.

In your example your arrays don't match: - $secondary_array[0] doesn't exist so $main_array[0] will not be populated with a 'gender' value; - $main_array[2] doesn't exist so $main_array[2] will be created and it will only have a 'gender' value same as $secondary_array[2]['gender']


If you want to only merge some bits and pieces of the arrays:

Rolling your own:

foreach($main_array as $i => &$main) {
    if(isset($secondary_array[$i])) and isset($secondary_array[$i]['gender'])) {
        $main['gender'] = $secondary_array[$i]['gender'];
    }
}
2
  • Ok sir I will try it but what if I have another element in the second array? How can I select only the gender?
    – Jerielle
    Feb 13, 2014 at 8:19
  • Please update the question with these specifications. Feb 13, 2014 at 8:23
0
foreach($main_array as &$main){//this is the loop inside the first array
    foreach($second_array as &$second){ //this is the loop inside the second array

    }
}
-1
foreach($arr1 as $k => $arr1Item) {
   $arr1[$k]['gender'] = $arr2[$k]['gender'];
}

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.