-1

I have two arrays.

$a=array('caption'=>array('c one','c two'));
$b=array('photos'=>array('photo one','photo two'));

I want to merge into

Array (
    [0] => Array (
        [caption] => c one
        [file] => photo one
    )
    [1] => Array (
        [caption] => c two
        [file] => photo two
    )
) 

or merge into

Array (
    [0] => Array (
        [0] => c one
        [1] => photo one
    )
    [1] => Array (
        [0] => c two
        [1] => photo two
    )
) 

How do you do it?

FirstOne
  • 6,033
  • 7
  • 26
  • 45
Phyo
  • 31
  • 1
  • 9

3 Answers3

1

You can use a nested loop, and just keep track of your keys.

foreach ([$a, $b] as $array) {
    foreach ($array as $text_key => $values) {
        foreach ($values as $numeric_key => $value) {
            $result[$numeric_key][$text_key] = $value;
        }
    }
}
Don't Panic
  • 41,125
  • 10
  • 61
  • 80
1

The last case can be done by array_map function

$res = array_map(null, $a['caption'], $b['photos']);

demo

splash58
  • 26,043
  • 3
  • 22
  • 34
0

If the arrays does not have more in common than the key values then this should work.

It loops one array and uses the key to grab the value from the other array.

$a=array('caption'=>array('c one','c two'));
$b=array('photos'=>array('photo one','photo two'));


Foreach($a['caption'] as $key => $capt){
    $new[$key]['caption'] = $capt;
    $new[$key]['photos'] = $b['photos'][$key];
}

Var_dump($new);

Output:

array(2) {
  [0] => array(2) {
    ["caption"] => "c one"
    ["photos"] => "photo one"
  }
  [1] => array(2) {
    ["caption"] => "c two"
    ["photos"] => "photo two"
   }
}

https://3v4l.org/kfsft

Andreas
  • 23,610
  • 6
  • 30
  • 62
  • This works. what do you mean by "If the arrays does not have more in common than the key values". Can u give sample code to clarify this. – Phyo Nov 22 '17 at 09:16
  • Well. Is there any other relation between c one and photo one? If c one was last in array and photo one is first in other array are they still related or is it only the key index? – Andreas Nov 22 '17 at 09:19
  • i think it's only key index. if c one is last,the output array will be caption=>c one and photos=>photo two. if there is relation between c one and photo one,i think merging array will be complex if c one is last in array and photo one is first in other array and how will code know if c one is related to photo one. – Phyo Nov 22 '17 at 13:57
  • If you need that it can be done. But you need to give us real data instead. What is photo one and c one really? – Andreas Nov 22 '17 at 14:04
  • I have a download template using jquery file upload. if i upload a file, the file 1 name appears and a caption box 1 appears,if i upload another file,file 2 name appears below file 1 name and caption box 2 appears below caption box1. when i submit form, file names are pushed in photos array with javascript in the order that appears in template,and are $_POST to script along with captions. So in $_POST script i get two arrays, photos array and captions array. ex array([photos]=>array(0->file1,1->file2)),array([captions]=>array(0->c one,1->c two)). if u are unclear, let me know. – Phyo Nov 24 '17 at 15:43
  • In that case you won't need to find a relationship between photos and captions since it's controlled by the upload. – Andreas Nov 24 '17 at 17:03
  • but i have made the files sortable,so $_POST data can be photos=>array(0->file2,1->file1) and captions is captions(0->c one,1->c two) so if i merge array the data is wrong as this results,caption->c one ,photos->file2. I should be c one is for file1 and c two is for file2. – Phyo Nov 24 '17 at 17:19
  • Then don't sort the array. And even if you must, I still need real data to help you. – Andreas Nov 24 '17 at 17:21