1

I have three array like this:

array1(
   0=>'title 1',
   1=>'title 2'
)

array2(
   0=>'description 1',
   1=>'description 2'
)

array3(
   0=>'price 1',
   1=>'price 2'
)

There is a php function to grouping array values by keys like this?

array(
   0=>array(title 1, description 1, price 1),
   1=>array(title 2, description 2, price 2),
)
Gambric
  • 189
  • 1
  • 1
  • 12

3 Answers3

6
array_map(null, $array1, $array2, $array3)

See http://php.net/manual/en/function.array-map.php example #4.

wonce
  • 1,893
  • 12
  • 18
0

Try this code.

$array3 = array();
foreach ( $array1 as $key => $val ) {
    if ( !isset($array3[$val]) )
        $array3[$val] = array();

    $array3[$val][] = $array2[$key];
}

print_r($array3);
Kumar V
  • 8,810
  • 9
  • 39
  • 58
0
$pool =array();
foreach ( array_map(null, $array1, $array2, $array3) as $key => $value) {
    $pool[$key] = implode(", ", $value);
}
print_r($pool);

Result :

Array
(
    [0] => title 1, description 1, price 1
    [1] => title 2, description 2, price 2
)
Alireza Fallah
  • 4,609
  • 3
  • 31
  • 57