-1
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  ) 
 )) 

I have a array like below. I want to get below array by that array.

 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  ) 
 )

How to get this 2nd array by changing the 1st array. Help me.

Cœur
  • 37,241
  • 25
  • 195
  • 267
tenten
  • 1,276
  • 2
  • 26
  • 54
  • 5
    You are expected to try to **write the code yourself**. After [**doing more research**](https://meta.stackoverflow.com/q/261592/1011527) if you have a problem **post what you've tried** with a **clear explanation of what isn't working** and provide [a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve). Read [How to Ask](http://stackoverflow.com/help/how-to-ask) a good question. Be sure to [take the tour](http://stackoverflow.com/tour) and read [this](https://meta.stackoverflow.com/q/347937/1011527). – Jay Blanchard May 30 '17 at 03:43
  • Possible duplicate of [PHP append one array to another (not array\_push or +)](https://stackoverflow.com/questions/4268871/php-append-one-array-to-another-not-array-push-or) - just use `$foo.data` as the arguments. – Ken Y-N May 30 '17 at 03:44
  • 1
    in your first array, `report_id` for both array object is different... If you really want to combine two arrays into one then it is wrongly referenced to `report_id`. – Nidhi May 30 '17 at 03:52
  • @Nidhi It is okay. I don't need that report id – tenten May 30 '17 at 03:53

1 Answers1

0

This is my conclusion from what I understand

Combine date_stamp with same user_id

Try this

<?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);
?>

See demo here

Nidhi
  • 1,529
  • 18
  • 28