0

i have problem with array, i have 3 array and i wont to merge into one by row index

$tahun = array(2010,2011,2012,2013,2014);
$status = array("akademi","instansi","umum","pending","pass");
$total = array(2,1,3,4,5);

i want this array into want like this

array(
0 => array(2010,"akademi",2),
1 => array(2011,"instansi",1),
2 => array(2012,"umum",3),
3 => array(2013,"pemding",4),
4 => array(2014,"pass",5),

);

but when i use array_merge_recursive() the output like this

Array
(
    [0] => 2015
    [1] => 2016
    [2] => 2017
    [3] => 2018
    [4] => 2019
    [5] => 2019
    [6] => 2019
    [7] => 1
    [8] => 1
    [9] => 1
    [10] => 1
    [11] => 3
    [12] => 2
    [13] => 1
    [14] => akademisi
    [15] => instansi pemerintah
    [16] => umum
    [17] => umum
    [18] => akademisi
    [19] => instansi pemerintah
    [20] => umum
)

2 Answers2

1

Use simple foreach loop:

$res = [];
foreach($tahun as $ind=>$val){
    $res[$ind] = [$val, $status[$ind], $total[$ind]];
}

Demo

Aksen P
  • 4,564
  • 3
  • 14
  • 27
1

You can use array_map() with null as the callback...

$combined = array_map(null, $tahun, $status, $total);

from the manual...

NULL can be passed as a value to callback to perform a zip operation on multiple arrays. If only array1 is provided, array_map() will return the input array.

Nigel Ren
  • 56,122
  • 11
  • 43
  • 55