PHP - 使用基于特定键的所有值生成多维数组

时间:2016-01-08 08:10:55

标签: php arrays multidimensional-array

这是我的阵列:

sendMail

我要按如下方式转换此数组:

$arr = array(
  0 => array(
      'title' => 'test1',
      'count' => 4,
      'month' => 'jan-2015'
    ),
  1 => array(
      'title' => 'test2',
      'count' => 10,
      'month' => 'jan-2015'
    ),
  2 => array(
      'title' => 'test3',
      'count' => 14,
      'month' => 'jun-2015'
    ),
  3 => array(
      'title' => 'test4',
      'count' => 45,
      'month' => 'july-2015'
    ),
    4 => array(
      'title' => 'test1',
      'count' => 40,
      'month' => 'jun-2015'
    ),
);

即如果特定月份没有标题,那么我需要添加该标题,计数将为0.如何做到这一点? 我尝试过这个概念:

 $arr = array(
  'jan-2015' => array(
      0 => array(
        'title' => 'test1',
        'count' => 4,
      ),
      1 => array(
        'title' => 'test2',
        'count' => 10,
      ),
      2 => array(
        'title' => 'test3',
        'count' => 0,
      ),
      3 => array(
        'title' => 'test4',
        'count' => 0,
      ),
    ),
  'jun-2015' => array(
      0 => array(
        'title' => 'test1',
        'count' => 40,
      ),
      1 => array(
        'title' => 'test2',
        'count' => 0,
      ),
      2 => array(
        'title' => 'test3',
        'count' => 14,
      ),
      3 => array(
        'title' => 'test4',
        'count' => 0,
      ),
   ),
  'july-2015' => array(
      0 => array(
        'title' => 'test1',
        'count' => 0,
      ),
      1 => array(
        'title' => 'test2',
        'count' => 0,
      ),
      2 => array(
        'title' => 'test3',
        'count' => 0,
      ),
      3 => array(
        'title' => 'test4',
        'count' => 45,
     ),
   ),
);

但我无法得到我想要的东西。还有其他解决办法吗?

2 个答案:

答案 0 :(得分:0)

请根据您的期望尝试此代码。

$new_array = array();
foreach($arr as $each_arr){
    $month = $each_arr['month'];
    $new_array[$month][] = array(
        'title' => $each_arr['title'],
        'count' => $each_arr['count'],
    );
}

var_dump($new_array);

echo '<pre>';
print_r($new_array);
echo '</pre>';

答案 1 :(得分:0)

您可以尝试以下代码:

$arr = array(
    0 => array(
        'title' => 'test1',
        'count' => 4,
        'month' => 'jan-2015'
    ),
    1 => array(
        'title' => 'test2',
        'count' => 10,
        'month' => 'jan-2015'
    ),
    2 => array(
        'title' => 'test3',
        'count' => 14,
        'month' => 'jun-2015'
    ),
    3 => array(
        'title' => 'test4',
        'count' => 45,
        'month' => 'july-2015'
    ),
    4 => array(
        'title' => 'test1',
        'count' => 40,
        'month' => 'jun-2015'
    ),
);

$titles = array_unique( array_column( $arr, 'title' ) );
$months = array_unique( array_column( $arr, 'month' ) );

// Construct an array with the correct structure but with zero counts
$resultingArray = [];
foreach( $months as $month ){
    if( !is_array( $resultingArray[$month] ) ){
        $resultingArray[ $month ] = [];
    }
    foreach( $titles as $title ){
        $resultingArray[ $month ][] = ['title' => $title, 'count' => 0, 'month' => $month ];
    }
}
// And now populate the counts
foreach( $arr as $a ){
    foreach( $resultingArray as $month => &$tempArray ){
        foreach( $tempArray as &$array ){
            if( $a['month'] === $month && $a['title'] === $array['title'] ){
                $array['count'] = $a['count'];
            }
        }
    }
}

echo '<pre>';
print_r( $resultingArray );
echo '</pre>';