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),
)
2
  • This isn't one function to do this. You have to write your own. Commented Feb 2, 2014 at 16:20
  • is the number of arrays always 3 or can be more? Commented Feb 2, 2014 at 16:26

3 Answers 3

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

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

Sign up to request clarification or add additional context in comments.

Comments

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
)

Comments

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);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.