转换PHP多维数组

时间:2014-06-01 00:52:33

标签: php arrays

我想知道是否有任何PHP函数(或优雅的函数组合),类似于array_column,它允许转换此数组:

$my_array = array(
  0 => array(
      'film_id' => '19'
      'showing_id' => '525'
  )
  1 => array(
      'film_id' => '117'
      'showing_id' => '507'
  )
  2 => array(
      'film_id' => '19'
      'showing_id' => '526'
  )
  3 => array(
      'film_id' => '117'
      'showing_id' => '510'
  )
)

进入此数组(键现在是前一个数组的film_id,值是包含与每个showing_id关联的所有film_id的数组:

$new_array = array(
  '19' => array(
      0 => '525'
      1 => '526'
  )
  '117 => array(
      0 => '507'
      1 => '510'
  )
)

1 个答案:

答案 0 :(得分:1)

你可以试试这个:

$new_array = array();
foreach($my_array as $foo){
    $new_array[$foo['film_id']][] = $foo['showing_id'];
}