0

I have this issue with multidimensional arrays. I have an array having user login . Given the following multidimensional array:

                    $data = [
                    [
                        "_id" => "6318956768e7df9342bc8349",          
                        "userid" => "test1234AB", 
                        "lastlogindate" => "2022-10-31T12:21:23.000000Z",  
                    ], 
                    [
                        "_id" => "6318956768e7df9342bc8350",
                        "userid" => "test123456",
                        "lastlogindate" => "2022-08-31T12:21:23.000000Z",
                    ],
                    [
                        "_id" => "63357dfd5435bcdfae485685",
                        "userid" => "test1234AB",
                        "lastlogindate" => "2022-12-31T12:21:23.000000Z",

                    ] ,
                             
                    [
                      "_id" => "63357dfd5435bcdfae98316",  
                        "userid" => "xyz123", 
                        "lastlogindate" => "2022-12-31T12:21:23.000000Z",
                    
                    ] 
                ]; 

if having the same userid having multiple login so the lastlogindate will in same array(if userid) . I need output like

                Array
                (
                    [0] => Array
                        (
                            [_id] => 6318956768e7df9342bc8349
                            [userid] => test1234AB
                            [lastlogindate] => Array
                                (
                                    [0] => 2022-10-31T12:21:23.000000Z
                                    [1] => 2022-12-31T12:21:23.000000Z
                                )
                        )
                    [1] => Array
                        (
                            [_id] => 6318956768e7df9342bc8350
                            [userid] => test123456
                            [lastlogindate] => Array
                                (
                                    [0] => 2022-08-31T12:21:23.000000Z
                                )
                        )

                    [3] => Array
                        (
                            [_id] => 63357dfd5435bcdfae98316
                            [userid] => xyz123
                            [lastlogindate] => Array
                                (
                                    [0] => 2022-12-31T12:21:23.000000Z
                                )

                        )

                )


How can I do that??

gooddev
  • 1
  • 1
  • Simply walk thru the array1 , add data to array2 when there not exists the id (or append the lastlogin date in this array2 's lastdate array if exists) . By the way, what did you attempt ? (show your PHP code pls) – Ken Lee Oct 02 '22 at 02:38

1 Answers1

0

Use the userid as the array key to prevent duplicating entries. For example:

$data['testUserA']['lastlogindate'][] = "2022-07-31T12:21:23.000000Z";
$data['testUserA']['lastlogindate'][] = "2022-08-31T12:21:23.000000Z";
$data['testUserB']['lastlogindate'][] = "2022-09-31T12:21:23.000000Z";
$data['testUserC']['lastlogindate'][] = "2022-10-31T12:21:23.000000Z";

This will result in 3 items in the array (one for each unique user). The user testUserA will include both lastlogindate records.

verl
  • 116
  • 3
  • thank you for your response. see the thing is if in the array having same userid we group the lastlogindate push in one array in the output i have mistake wrote lastdate instead of lastlogindate. Actually i need how to achieve it. – gooddev Oct 02 '22 at 13:15
  • Ah; thanks for the clarification. You should be able to use the `userid` as the array key to group them appropriately. I've updated this answer to reflect this. – verl Oct 03 '22 at 03:50