简体   繁体   中英

Re index and sort a multidimensional PHP array

I am trying to Re index and Sort a multidimensional PHP array, keeping in mind that if some of the inside arrays do not have a particular index and others do, we want to just jump over to the next index in that particular inside array

Eg the source array below does not have array index 4 in subject_1 and subject_3 but has it in subject_2

            Array
            (
            [subject_1] => Array
            (
            [2] => a
            [6] => b
            )

            [subject_2] => Array
            (
            [2] => c
            [4] => d
            [6] => e
            )

            [subject_3] => Array
            (
            [2] => f
            [6] => g
            )
            )

So, the expected result array should be something like the below

            Array
            (
            [subject_1] => Array
            (
            [0] => a
            [2] => b
            )       
            [subject_2] => Array
            (
            [0] => c
            [1] => d
            [2] => e
            )

            [subject_3] => Array
            (
            [0] => f
            [2] => g
            )
            )
            )
            )

The issue with the below code is that it does know if the index exists on all sub arrays before sorting

            $all_incoming_keys = array();
            foreach($p2 as $key => $value){
            $all_incoming_keys[] = $key;
            }
            for($sp2 = 0; $sp2 < count($p2); $sp2++){
            sort($p2[$all_incoming_keys[$sp2]]);
            array_values($p2[$all_incoming_keys[$sp2]]);
            } 

It this code the right approach for the situation or is there a function that I am missing

To be able to reindex the keys based on the position is a list of unique keys, you first need to decide this order. This code goes through all subjects and merges all of the keys into 1 array ( $all_incoming_keys ). This is then processed to create a unique list of keys (in numerical order) and creates a list with an associated position.

$all_incoming_keys = [];
// Extract all of the keys
foreach ( $a as $subjects ) {
    $all_incoming_keys = array_merge ($all_incoming_keys, array_keys($subjects));
}
sort($all_incoming_keys);
// Create sequential list of different values
$all_incoming_keys = array_flip(array_values(array_unique($all_incoming_keys)));
print_r($all_incoming_keys);

This gives...

Array
(
    [2] => 0
    [4] => 1
    [6] => 2
)

The second part then just passes through the subjects again and copies the value for each item into a new array with the key from the above list...

$newA = [];
foreach ( $a as $subjectName => $subjects ) {
    foreach ( $subjects as $key => $subject ){
        $newA[$subjectName][$all_incoming_keys[$key]] = $subject;
    }
}

print_r($newA);

Which in your example, should give...

Array
(
    [subject_1] => Array
        (
            [0] => a
            [2] => b
        )

    [subject_2] => Array
        (
            [0] => c
            [1] => d
            [2] => e
        )

    [subject_3] => Array
        (
            [0] => f
            [2] => g
        )

)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM