Home > Mobile >  php multidimensional arrays. Change structure
php multidimensional arrays. Change structure

Time:11-18

Good day. I have the following problem:

I have an array.. I want to change stucture of it .. I do not want to put Array to parent Array if object_id not the same. How to do this? Array with object_id 917 should be level above like Arrays with index 1 and 2.

Is it possible at index 1 for example to have Array of arrays by different object_id ?

object_type it is like unique indexes.

Initial array:

$arr = [
    [
        'initiator_id' => 259, 'object_type' => 1, 'object_id' => 905,
        'date'         => '2021-11-16 06:24:16',
    ],
    [
        'initiator_id' => 259, 'object_type' => 1, 'object_id' => 917,
        'date'         => '2021-11-16 06:24:16',
    ],
    
    [
        'initiator_id' => 259, 'object_type' => 1, 'object_id' => 905,
        'date'         => '2021-11-16 04:54:54',
    ],
    [
        'initiator_id' => 259, 'object_type' => 1, 'object_id' => 905,
        'date'         => '2021-11-16 04:53:58',
    ],
    [
        'initiator_id' => 219, 'object_type' => 2, 'object_id' => 915,
        'date'         => '2021-11-16 04:53:58',
    ],
];

Array:

Array
(
    [object_type] => Array
        (
            [1] => Array
                (
                    [0] => Array
                        (
                            [initiator_id] => 259
                            [object_id] => 905
                            [date] => 2021-11-16 06:24:16
                            [activity] => added 
                        )

                    [1] => Array
                        (
                            [initiator_id] => 259
                            [object_id] => 917 
                            [date] => 2021-11-16 06:24:16
                            [activity] => deleted
                        )

                    [2] => Array
                        (
                            [initiator_id] => 259
                            [object_id] => 905
                            [date] => 2021-11-16 04:54:54
                            [activity] => reopen
                        )

                    [3] => Array
                        (
                            [initiator_id] => 259
                            [object_id] => 905
                            [date] => 2021-11-16 04:53:58
                            [activity] => insert
                        )

                )

            [2] => Array
                (
                    [0] => Array
                        (
                            [initiator_id] => 219
                            [object_id] => 915
                            [date] => 2021-11-16 04:53:58
                            [activity] => change
                        )

                )

        )

)

php code:

foreach($arr as $record) {
    foreach($record as $k => $v) {
        if($k == 'object_type') {
            $newArr['object_type'][$v][] = [
                'initiator_id' => $record['initiator_id'],
                'object_id' => $record['object_id'],
                'date' => $record['date'],
                ];
        }
    }
}

expected result:

[object_type] => Array
    (
        [1] => Array
            (
        
                [905] => Array
                    (
                        [1] => Array
                            (
                                 [initiator_id] => 219
                                [object_id] => 905
                                [date] => 2021-11-16 04:53:58
                                [activity] => change


                            )

                        [2] => Array
                            (
                                [initiator_id] => 219
                                [object_id] => 905
                                [date] => 2021-11-16 04:53:58
                                [activity] => change

                            )

                    )
                    
                    
                [917] => Array
                    (
                        [1] => Array
                            (
                                [initiator_id] => 219
                                [object_id] => 917
                                [date] => 2021-11-16 04:53:58
                                [activity] => change

                            )

                        [2] => Array
                            (
                                [initiator_id] => 219
                                [object_id] => 917
                                [date] => 2021-11-16 04:53:58
                                [activity] => change
                    
                            )

                    )
                    
                    
            )

        [2] => Array
            (

                [925] => Array
                    (
                        [1] => Array
                            (
                                [initiator_id] => 925
                                [object_id] => 917
                                [date] => 2021-11-16 04:53:58
                                [activity] => change
                            )


                    )

            )

    )

CodePudding user response:

Simply iterate over $arr and build a new array by putting each $arr item in its desired position:

$result = []; 
foreach($arr as $item) { 
    $result['object_type']
           [$item['object_type']]
           [$item['object_id']] []= $item; 
} 

print_r($result);

[]= means "add the right hand side to the array on the left hand side".

Edit Added object_id as 3rd level.

  • Related