foreach($result as $key=>$value){
$time=$value["e"]["start_date"];
   $time=date("Y",strtotime($time));
if(array_key_exists($time,$array_years)){
$temp=$array_years[$time];
$temp=$value;
$array_years=$temp;
}else{
$array_years=array($time=>$value);
}


}
判断键是否存在,如果存在,取出原来的键对应的数组,将value添加到数组里面,在吧该数组加到array_years
如果不存在,就以年份为键,value做值添加到array_years里面
现在好像有问题,覆盖了..

解决方案 »

  1.   

    foreach($result as $key=>$value){
    $time=$value["e"]["start_date"];
       $time=date("Y",strtotime($time));
    if(array_key_exists($time,$array_years)){
    $temp=$array_years[$time];
    $temp=$value;
    $array_years=array($time=>$temp);
    }else{
    $array_years=array($time=>array($value));
    }


    }
      

  2.   

    foreach($result as $key=>$value){
    $time=$value["e"]["start_date"];
    $time=date("Y",strtotime($time));
    if(array_key_exists($time,$array_years)){
    $temp=$array_years[$time];
    $temp=$value;

    $array_years=$temp;
    }else{
    $array_years=array($time=>$value);
    }
    }那两句有问题
      

  3.   

    代码怎么看不懂就说一点,添加进数组应该加个这个[]$temp=$array_years[$time];
    $temp[]=$value;
    $array_years=$temp;
      

  4.   

    foreach($result as $key=>$value){
    $time=$value["e"]["start_date"];
    $time=date("Y",strtotime($time));
    if(array_key_exists($time,$array_years)){
    array_push($array_years[$time], $valu);
    }else{
    $array_years=array($time=>$value);
    }
    }不知道你是不是要这种效果
      

  5.   

    foreach($result as $key=>$value){
      $time=date("Y",strtotime($value["e"]["start_date"]));
      $array_years[$time] = $value;
    }
      

  6.   

    $temp=$array_years[$time];
    $temp=$value; //你把先赋的值给覆盖掉了,所以前面那句无意义
    $array_years=$temp;//这句就更牛了,你把整个$array_years都重新赋值了
      

  7.   


    结果如下
    Array ( [2008] => Array ( [e] => Array ( [id] => 83 [title] => 镜头问卷 [start_date] => 2008-10-01 ) [0] => Array ( [id] => 9 [title] => D60 D90用户问卷 [start_date] => 2008-10-01 ) [1] => Array ( [id] => 36 [title] => D40X问卷 [start_date] => 2008-10-01 ) ) [2009] => Array ( [e] => Array ( [id] => 92 [title] => 注册问卷调查测试标题 [start_date] => 2009-04-01 ) [0] => Array ( [id] => 89 [title] => D3 [start_date] => 2009-03-02 ) [1] => Array ( [id] => 98 [title] => Vincent 18-135 [start_date] => 2009-04-13 ) [2] => Array ( [id] => 97 [title] => Vincent D90 [start_date] => 2009-04-13 ) [3] => Array ( [id] => 88 [title] => xuhua_test [start_date] => 2009-03-02 ) [4] => Array ( [id] => 85 [title] => アンケートテスト [start_date] => 2009-03-02 ) [5] => Array ( [id] => 91 [title] => title [start_date] => 2009-03-02 ) [6] => Array ( [id] => 96 [title] => Vincent 70-200mm [start_date] => 2009-04-13 ) ) ) 添加进去的索引是从0开始的,我像让他跟第一个元素一样,不需要0,只要键都是e,
    怎么弄啊
    这是代码:foreach($result as $key=>$value){
    $time=$value["e"]["start_date"];
       $time=date("Y",strtotime($time));
    if(array_key_exists($time,$array_years)){
    $temp=$array_years[$time];
    $temp[]=$value["e"];
    $array_years[$time]=$temp;
    }else{
    $array_years[$time]=$value;
    }


    }