0

I have the following arrays which I'd like to combine / merge to create a single array that I can loop through to add images and their respective titles to a database.

Array 1:

[0] => array(1) {
  ["filename"] => string(22) "1463668615_1_image.jpg"
}
[1] => array(1) {
  ["filename"] => string(22) "1463668641_1_image.jpg"
}

Array 2:

[0] => array(1) {
  ["title"] => string(15) "My image title"
}
[1] => array(1) {
  ["title"] => string(5) "Title"
}

Here's the format of the array I'd like to create.

Merged Arrays:

[0] => array(2) {
  ["filename"] => string(22) "1463668615_1_image.jpg",
  ["title"] => string(3) "My image title"
}
[1] => array(2) {
  ["filename"] => string(22) "1463668641_1_image.jpg",
  ["title"] => string(0) "Title"
}
Jim Pannell
  • 147
  • 6

4 Answers4

2

If I understand you correctly this called zip, not merge. You can do this with array_map() function:

$filenames = [
    ['filename' => '1463668615_1_image.jpg'],
    ['filename' => '1463668641_1_image.jpg']
];

$titles = [
    ['title' => 'Title1'],
    ['title' => 'Title2']
];

$zipped = array_map(function ($elem1, $elem2) {
    return [
        'filename' => $elem1['filename'],
        'title' => $elem2['title']
    ];
}, $filenames, $titles);

var_dump($zipped);

Here is demo.

sevavietl
  • 3,762
  • 1
  • 14
  • 21
2

array_merge() is the way just use it as the callback in array_map():

$result = array_map('array_merge', $array1, $array2);
AbraCadaver
  • 78,200
  • 7
  • 66
  • 87
1

Will the structure of the arrays be the same? And will the size of those arrays also always be the same? If not, there is no sure way to do this, if yes, you can do a simple loop:

$arr1 = array(
    0 => array(
        "filename" => "1463668615_1_image.jpg",
    ),
    1 => array(
        "filename" => "1463668641_1_image.jpg",
    ),
);

$arr2 = array(
    0 => array(
        "title" => "My image title",
    ),
    1 => array(
        "title" => "Title",
    ),
);

$new = array();
for ($k = 0, $size = count($arr1); $k < $size; $k++) {
    $new[$k]['filename'] = $arr1[$k]['filename'];
    $new[$k]['title'] = $arr2[$k]['title'];
}

var_dump($new);
Peon
  • 7,902
  • 7
  • 59
  • 100
  • @Daninis Put the count outside the loop, it's an unnecessary overhead! – Edward May 19 '16 at 15:25
  • I declined your edit, since it was not, what I intended to show. You can check the manual here: http://php.net/manual/en/control-structures.for.php – Peon May 19 '16 at 15:33
  • yes either way will do, I prefer to set the variable explicitly outside the loop because it's more readable but my 'Wait for it...' point was you are comparing `$i` not `$k` you rebel! – Edward May 19 '16 at 15:37
  • @DainisAbols I would hope you test your own code. Its your answer not mine. – CodeGodie May 19 '16 at 15:45
0

This should work :

PHP

    $filenames = [["filename" => "1463668615_1_image.jpg"], ["filename" => "146366865213_image.jpg"]];
    $titles = [["title" => "Titre1"], ["title" => "Titre22"]];

    foreach ($filenames as $key => &$value) {
         $value['title'] = $titles[$key]['title'];
    }

    var_dump($filenames);
Sofiene Djebali
  • 4,398
  • 1
  • 21
  • 27