-1

I need help to create array inside array. I have this 2 variables containing array:

$first = [12, 23, 34];
$second = [32, 21, 43];

I want to create an array from that two arrays. The form of array that I want is:

$combine = {'name'=>'data1', [[12,32], [23,21], [34,43]]}

I have tried this one:

    $first = [12, 23, 34];
    $second = [32, 21, 43];
    $comb=[];
    foreach($first as $key){
        foreach($second as $row){
            $comb[]=$key;
            $comb[]=$row;
        }
    }
    $combine=['name'=>'data1', 'color'=> '#299662', $comb];

and it result in this array:

enter image description here

1 Answers1

2

Simple solution is:

$first = [12, 23, 34];
$second = [32, 21, 43];
$combine = ['name'=>'data1', 'color'=> '#299662', array_map(null, $first, $second)];
print_r($combine);
u_mulder
  • 54,101
  • 5
  • 48
  • 64