修改php multidimentional数组

时间:2017-05-30 03:41:15

标签: php arrays

Array ( 
[0] => Array ( 
[user_id] => 1 
[report_id] => 8
[data] => Array ( 
[0] => Array ( [date_stamp] => 03/04/2017  ) 
[1] => Array ( [date_stamp] => 04/04/2017  ) 
 )
 [1] => Array ( 
[user_id] => 1 
[report_id] => 5 
[data] => Array ( 
[0] => Array ( [date_stamp] => 09/04/2017  ) 
[1] => Array ( [date_stamp] => 06/04/2017  ) 
 )) 

我有一个如下所示的数组。  我希望通过该数组获得数组。

 Array ( 
[0] => Array ( 
[user_id] => 1 
[report_id] => 8
[data] => Array ( 
[0] => Array ( [date_stamp] => 03/04/2017  ) 
[1] => Array ( [date_stamp] => 04/04/2017  ) 
[2] => Array ( [date_stamp] => 09/04/2017  ) 
[3] => Array ( [date_stamp] => 06/04/2017  ) 
 )

如何通过更改第一个数组来获取第二个数组。帮助我。

1 个答案:

答案 0 :(得分:0)

根据我的理解,这是我的结论

date_stamp与相同的user_id结合使用

试试这个

<?php
$a = array(array("user_id"=>1,"report_id"=>8,"data"=>array(array("date_stamp"=>"03/04/2017"),array("date_stamp"=>"04/04/2017"))),
array("user_id"=>1,"report_id"=>5,"data"=>array(array("date_stamp"=>"09/04/2017"),array("date_stamp"=>"06/04/2017"))),
array("user_id"=>2,"report_id"=>5,"data"=>array(array("date_stamp"=>"19/04/2017"),array("date_stamp"=>"16/04/2017"))),
array("user_id"=>2,"report_id"=>5,"data"=>array(array("date_stamp"=>"29/04/2017"),array("date_stamp"=>"26/04/2017"))));

$user_id = 0;
$arr = array();
foreach($a as $b){
    $temp_array = array();
    if($user_id == $b['user_id']){
        foreach($b['data'] as $data){
            array_push($arr[$user_id]['data'],$data);
        }

    }else{

        $temp_array['data'] = $b['data'];
        $temp_array['user_id'] = $b['user_id'];
        $user_id = $b['user_id'];
        $arr[$user_id] = $temp_array;
    }
}
echo '<pre>';
print_r($arr);
?>

请参阅demo此处