PHP - 生成多维数组

时间:2016-01-07 12:23:54

标签: php arrays multidimensional-array

这是我的阵列:

$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'
    ),
);

我将此数组转换为多维数组,如下所示:

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

我试图按预期做到,但不幸的是我无法做到这一点。 还有其他解决办法吗?

4 个答案:

答案 0 :(得分:2)

您可以使用此功能:

function transform($input) {
    // Extract months, and use them as keys, with value set to empty array
    // The array_fill_keys also removes duilicates
    $output = array_fill_keys(array_column($input, 'month'), array());

    foreach ($input as $element) {
        $copy = $element;
        // remove the month key
        unset($copy["month"]);
        // assign this to the month key in the output 
        $output[$element["month"]][] = $copy;
    }
    return $output;
}

这样称呼:

$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'
    ),
);

print_r (transform($arr));

输出:

Array
(
    [jan-2015] => Array
        (
            [0] => Array
                (
                    [title] => test1
                    [count] => 4
                )
            [1] => Array
                (
                    [title] => test2
                    [count] => 10
                )
        )
    [jun-2015] => Array
        (
            [0] => Array
                (
                    [title] => test3
                    [count] => 14
                )
        )
    [july-2015] => Array
        (
            [0] => Array
                (
                    [title] => test4
                    [count] => 45
                )
        )
)

答案 1 :(得分:2)

根据您的数据结构:

$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'
    ),
);

试试这个:

$newArray = array();
foreach($arr as $key => $val) {
    $newArray[$val['month']][] = $val;
}
echo '<pre>'.print_r($newArray,1).'</pre>';

输出:

Array
(
    [jan-2015] => Array
        (
            [0] => Array
                (
                    [title] => test1
                    [count] => 4
                    [month] => jan-2015
                )

            [1] => Array
                (
                    [title] => test2
                    [count] => 10
                    [month] => jan-2015
                )

        )

    [jun-2015] => Array
        (
            [0] => Array
                (
                    [title] => test3
                    [count] => 14
                    [month] => jun-2015
                )

        )

    [july-2015] => Array
        (
            [0] => Array
                (
                    [title] => test4
                    [count] => 45
                    [month] => july-2015
                )

        )

)

答案 2 :(得分:1)

通过使用@Girish Patidar的答案,您可以通过以下方式实现这一目标:

$outputArr = array();
$to_skip = array();

foreach($arr as $row){
  $to_skip = $row;
  unset($to_skip['month']);
  $outputArr[$row['month']][] = $to_skip;
}

echo "<pre>";
print_r($outputArr);
die;

答案 3 :(得分:1)

有很多方法可以做到这一点。如果它适合你,请尝试这个

<?php
$newArr=NULL;
foreach($arr as $array)
{
    $temp=NULL;
    $temp['title']=$array['title'];
    $temp['count']=$array['count'];

    $newArr[$array['month']][]=$temp;
}

var_dump($newArr);
?>