php 数组分组

<?php
$a=array(0=>array('id'=>1,'tt'=>'asdfasdf','time'=>'2013-1-2'),1=>array('id'=>2,'tt'=>'asdfasdf','time'=>'2013-1-2'),2=>array('id'=>3,'tt'=>'asdddfffasdf','time'=>'2013-1-3'));

$data=array();

foreach ($a as $key => $value) {
	$data[$value['time']][]=$value;
}
var_dump($a);

var_dump($data);
$end=array();
$i=0;
foreach($data as $key=>$v){
	$end[$i]['date']=$key;
	$end[$i]['content']=$v;
	$i+=1;
}

var_dump($end);

结果:

array(3) {
  [0]=>
  array(3) {
    ["id"]=>
    int(1)
    ["tt"]=>
    string(8) "asdfasdf"
    ["time"]=>
    string(8) "2013-1-2"
  }
  [1]=>
  array(3) {
    ["id"]=>
    int(2)
    ["tt"]=>
    string(8) "asdfasdf"
    ["time"]=>
    string(8) "2013-1-2"
  }
  [2]=>
  array(3) {
    ["id"]=>
    int(3)
    ["tt"]=>
    string(12) "asdddfffasdf"
    ["time"]=>
    string(8) "2013-1-3"
  }
}
array(2) {
  ["2013-1-2"]=>
  array(2) {
    [0]=>
    array(3) {
      ["id"]=>
      int(1)
      ["tt"]=>
      string(8) "asdfasdf"
      ["time"]=>
      string(8) "2013-1-2"
    }
    [1]=>
    array(3) {
      ["id"]=>
      int(2)
      ["tt"]=>
      string(8) "asdfasdf"
      ["time"]=>
      string(8) "2013-1-2"
    }
  }
  ["2013-1-3"]=>
  array(1) {
    [0]=>
    array(3) {
      ["id"]=>
      int(3)
      ["tt"]=>
      string(12) "asdddfffasdf"
      ["time"]=>
      string(8) "2013-1-3"
    }
  }
}
array(2) {
  [0]=>
  array(2) {
    ["date"]=>
    string(8) "2013-1-2"
    ["content"]=>
    array(2) {
      [0]=>
      array(3) {
        ["id"]=>
        int(1)
        ["tt"]=>
        string(8) "asdfasdf"
        ["time"]=>
        string(8) "2013-1-2"
      }
      [1]=>
      array(3) {
        ["id"]=>
        int(2)
        ["tt"]=>
        string(8) "asdfasdf"
        ["time"]=>
        string(8) "2013-1-2"
      }
    }
  }
  [1]=>
  array(2) {
    ["date"]=>
    string(8) "2013-1-3"
    ["content"]=>
    array(1) {
      [0]=>
      array(3) {
        ["id"]=>
        int(3)
        ["tt"]=>
        string(12) "asdddfffasdf"
        ["time"]=>
        string(8) "2013-1-3"
      }
    }
  }
}

发表评论

电子邮件地址不会被公开。 必填项已用*标注