如何安排多维数组 - 在PHP中按值分组?

时间:2016-01-04 07:02:58

标签: php arrays multidimensional-array

我想将相同的日期项目安排到单个索引,我有以下数组 -

Array(
[0] => Array
    (
        [date] => 30 Dec 2015
        [record] => Array
            (
                [id] => 84675
                [name] => Item1
            )
    )
[1] => Array
    (
        [date] => 28 Dec 2015
        [record] => Array
            (
                [id] => 84675
                [name] => item2
            )
    )
[2] => Array
    (
        [date] => 22 Nov 2015
        [record] => Array
            (
                [id] => 2011
                [name] => item3
            )
    )
[3] => Array
    (
        [date] => 22 Nov 2015
        [record] => Array
            (
                [id] => 86649
                [name] => item4
            )
    ))

我想安排这个数组 -

Array(
[0] => Array
    (
        [date] => 30 Dec 2015
        [record] => Array
            (
                [id] => 84675
                [name] => Item1
            )
    )
[1] => Array
    (
        [date] => 28 Dec 2015
        [record] => Array
            (
                [id] => 84675
                [name] => item2
            )
    )
[2] => Array
    (
        [date] => 22 Nov 2015
        [record] => Array
            (
                [id] => 2011
                [name] => item3
            ),Array
            (
                [id] => 86649
                [name] => item4
            )
    )
)

我想将相同的日期项目安排到单个索引中,任何人都可以帮助我。我试图用循环来安排它但是不能获得成功。任何帮助都会非常感激。

谢谢!

2 个答案:

答案 0 :(得分:2)

尝试使用此代码可以提供帮助

<?php 
  $result = array();
  foreach($data as $info)
  {
    $result[$info['date']][] = $info;
  }
  print_r($result);
?>

答案 1 :(得分:0)

你也可以这样做。通过这种方式,您将能够获取$ record_array键中的日期并以值记录值。试试这个..

0

希望这将有所帮助