Custom sort a multidimensional array by subarray keys using another array [duplicate]
Asked Answered
I

1

1

I have 2 arrays. Array1 looks something like this (which is just a small example, in reality it haves more then 408 rows):

Array
(
    [0] => Array ( [536870925] => 34213897 )
    [1] => Array ( [536870923] => 34213905 )
    [2] => Array ( [536870923] => 34213913 )
    [3] => Array ( [536870928] => 34213921 )
    [4] => Array ( [536870926] => 34213929 )
    [5] => Array ( [536870919] => 34213937 )
    [6] => Array ( [536870918] => 34218041 )
    [7] => Array ( [536870925] => 34218049 )
    [8] => Array ( [536870929] => 34218057 )
    [9] => Array ( [536870920] => 34218065 )
    [10] => Array ( [536870920] => 34218073 )
)

And Array2 looks something like this and it has only 16 rows:

Array
(
    [0] => 536870922
    [1] => 536870923
    [2] => 536870924
    [3] => 536870925
    [4] => 536870926
    [5] => 536870927
    [6] => 536870928
    [7] => 536870929
    [8] => 536870914
    [9] => 536870915
    [10] => 536870916
    [11] => 536870917
    [12] => 536870918
    [13] => 536870919
    [14] => 536870920
    [15] => 536870921
)

All of the ids that are in array2 exist in array1.

Both array1 and array2 are collected trow snmp_get from two different indexes. I need to reorder the ids and tthir equivalent value from array1 to match the id order that are presented in array2.

I tried to use a foreach with array1 but using array2 as the controller something like this, obviously this did not work..

$i = 0;
foreach ($intmac_ids as $value) {
        echo $value[$unique_mac_id[$i++]];
}

The output that I am looking for is like this:

Array
    (
        [0] => Array ( [536870923] => 34213905 )
        [1] => Array ( [536870923] => 34213913 )
        [2] => Array ( [536870925] => 34213897 )
        [3] => Array ( [536870925] => 34218049 )
        [4] => Array ( [536870926] => 34213929 )
        [5] => Array ( [536870928] => 34213921 )
        [6] => Array ( [536870929] => 34218057 )
        [7] => Array ( [536870918] => 34218041 )
        [8] => Array ( [536870919] => 34213937 )
        [9] => Array ( [536870920] => 34218065 )
        [10] => Array ( [536870920] => 34218073 )
    )
Interlinear answered 16/1, 2019 at 0:8 Comment(6)
I don't think I understand what you're trying to do. What is the second array needed for? Could you provide a smaller example with the desired output?Cloraclorinda
You have multiple values in array1 for some keys e.g. 536870914. Which one do you want to return?Vendace
Basically I need array1 valus to be in the order of the ids in array2 so i can make a new array based of the order of array2Interlinear
Both array1 and array2 are collected trow snmp_get from two different indexes. I need to re order the ids and there equvelent value from array1 to the order that the ids are presented in array2Interlinear
Do all ids exist in both arrays? Please update your question with your desired output.Assamese
Yes that's correct the same ids exist in both arraysInterlinear
A
3

You can use usort() on $array1 and pass the $array2 in as the sort order lookup array.

Code: (Demo)

$array1 = [
    [536870914 => 34213897],
    [536870914 => 34213905],
    [536870915 => 34213921],
    [536870914 => 34213913],
    [536870915 => 34213929],
    [536870917 => 34213937],
    [536870925 => 34218049],
];

$array2 = [
    536870925,
    536870914,
    536870915,
    536870917,
];

usort($array1, function($a, $b) use ($array2) {
    return array_search(key($a), $array2) <=> array_search(key($b), $array2);
});

var_export($array1);

To make this process more efficient, flip the lookup array in advance.

Code: (Demo)

$array2 = array_flip($array2);

usort($array1, function($a, $b) use ($array2) {
    return $array2[key($a)] <=> $array2[key($b)];
});

var_export($array1);

If some ids from array1 are not represented in array2, you will need to determine how these outliers should be sorted. If you do not adjust for missing values in the lookup array, you will generate Notices and have an unexpected sorting outcome.

Here is a similar post that offers some guidance on that subject:

https://mcmap.net/q/261457/-sorting-arrays-of-objects-by-predefined-map-of-values-duplicate

Assamese answered 16/1, 2019 at 1:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.