简体   繁体   English

重新索引并排序多维 PHP 数组

[英]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我正在尝试重新索引和排序多维 PHP 数组,请记住,如果 arrays 内部的一些没有特定索引而其他有,我们只想跳到该特定内部数组中的下一个索引

Eg the source array below does not have array index 4 in subject_1 and subject_3 but has it in subject_2例如,下面的源数组在 subject_1 和 subject_3 中没有数组索引 4,但在 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下面代码的问题是它在排序之前知道索引是否存在于所有子 arrays

            $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这段代码是针对这种情况的正确方法,还是我缺少 function

To be able to reindex the keys based on the position is a list of unique keys, you first need to decide this order.为了能够根据 position 是唯一键列表重新索引键,您首先需要确定此顺序。 This code goes through all subjects and merges all of the keys into 1 array ( $all_incoming_keys ).此代码遍历所有主题并将所有键合并到 1 个数组( $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.然后对其进行处理以创建唯一的键列表(按数字顺序)并创建一个具有关联 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
        )

)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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