0

I have 5 differents arrays in my script :

$array1 = array(

array( "id"=>"1", "title"=>"Batman" ),
array( "id"=>"2", "title"=>"Spiderman" ),
array( "id"=>"3", "title"=>"Titanic" ),
array( "id"=>"4", "title"=>"Dracula" ),

);

$array2 = array(

array( "id"=>"1", "releasedate"=>"1926" ),
array( "id"=>"2", "releasedate"=>"1956" ),
array( "id"=>"3", "releasedate"=>"2001" ),
array( "id"=>"4", "releasedate"=>"1982" ),

);

etc ... As you see, info about movie number 1 is splitted on all the arrays (in fact -> 5 arrays). Then I would like to merge all my arrays to get something like this :

$array_FINAL = array(

array( "id"=>"1", "title"=>"Batman", "releasedate"=>"1926" ),
array( "id"=>"2", "title"=>"Spiderman", "releasedate"=>"1956" ),
array( "id"=>"3", "title"=>"Titanic", "releasedate"=>"2001" ),
array( "id"=>"4", "title"=>"Dracula", "releasedate"=>"1982" ),

);

I tried array_merge, array_combine, no good results. I also checked other topics on stackoverflow but no one help me (i may miss the one i need!)

Any help ? :)

EDIT : Sorry, i did would give a little more details ... rows in the arrays could be in misc. order, then according to my code example : the movie "Batman" can be in 1st row in the first array, but in the 3rd row in the second array...

4
  • You can first index the arrays by id, and then iterate over them, combining arrays that have the same key. Commented Aug 4, 2017 at 16:01
  • In a simple way you can't do it. Commented Aug 4, 2017 at 16:01
  • 1
    If from a DB you could get this all in one query. Commented Aug 4, 2017 at 16:07
  • Sorry, i did would give a little more details ... rows in the array could be in misc. order, then according to my code example : the movie "Batman" can be in 1st row in the first array, but in the 3rd row in the second array... Commented Aug 7, 2017 at 8:03

4 Answers 4

2

If the order of both arrays is the same, you can simply use

$array_final = array_replace_recursive($array1, $array2);

However, if you want to merge them by the "id", you need to loop through them. A solution with the complexity O(m*n):

$array_final = array();
// loop through all movies in array1
foreach($array1 as $movie){
    foreach($array2 as $movie_release) {
        if ($movie['id'] == $movie_release['id']) {
            $movie['releasedate'] = $movie_release['releasedate'];
            break;
        }
    }
    $array_final[] = $movie;
}

And a little less complex O(m+n):

// create arrays with the ID as key
$titles = array_column($array1, 'title', 'id');
$releasedates = array_column($array2, 'releasedate', 'id');

$array_final = array();

foreach($titles as $id => $title) {
    $array_final[] = array(
        'id' => $id,
        'title' => $title,
        'releasedate' => $releasedates[$id]
    );
}
Sign up to request clarification or add additional context in comments.

Comments

1

One of the solutions is:

$array1 = array(

array( "id"=>"1", "title"=>"Batman" ),
array( "id"=>"2", "title"=>"Spiderman" ),
array( "id"=>"3", "title"=>"Titanic" ),
array( "id"=>"4", "title"=>"Dracula" ),

);

$array2 = array(

array( "id"=>"1", "releasedate"=>"1926" ),
array( "id"=>"2", "releasedate"=>"1956" ),
array( "id"=>"3", "releasedate"=>"2001" ),
array( "id"=>"4", "releasedate"=>"1982" ),

);

// here we create pairs `id => releasedate`    
$new_array2 = [];
foreach ($array2 as $v) {
    $new_array2[$v['id']] = $v['releasedate'];
}

foreach ($array1 as &$value) {
    // here we try to find key `$value['id']` in `$new_array2`
    // and get it's value
    if (isset($new_array2[$value['id']])) {
        $value['releasedate'] = $new_array2[$value['id']];
    }
}

If you're 100% sure that orders of ids are the same in both arrays you can just:

$i = 0;
foreach ($array1 as &$value) {
    $value['releasedate'] = $array2[$i]['releasedate'];
    $i++;
}

1 Comment

Sorry, i did would give a little more details ... rows in the array could be in misc. order, then according to my code example : the movie "Batman" can be in 1st row in the first array, but in the 3rd row in the second array...
0

See this code prototype (it is simple and don't need to explanations I believe):

$fArr = [];
for ( $i = 1; $i <= 4; $i++ ) {
    $fArr[] = [
        'id' => $i,
        'title' => $array1[$i - 1]['title'],
        'releasedate' => $array2[$i - 1]['releasedate'],
    ];
}

Sandbox code example

1 Comment

Sorry, i did would give a little more details ... rows in the array could be in misc. order, then according to my code example : the movie "Batman" can be in 1st row in the first array, but in the 3rd row in the second array...
0

Assuming all of your five arrays contain the id key, you can do this with a nested foreach loop.

foreach (array_merge($array1, $array2, $array3, $array4, $array5) as $item) {
    foreach ($item as $key => $value) {
        $result[$item['id']][$key] = $value;
    }
}

The outer foreach iterates all the rows in all the arrays. The inner one loops over each column and assigns its value to the corresponding key in the result array.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.