0

I have 2 arrays. My First array is:

$a1 = array("red010", "blue210", "green101", "black210", "orange120");

My Second array is:

$a2 = array(
    'arr1' =>
    array(
        'id' => 128,
        'level' => 1,
        'name' => 'blue210',
    ),
    'arr2' =>
    array(
        'id' => 220,
        'level' => 2,
        'name' => 'green101',
    ),
    'arr3' =>
    array(
        'id' => 124,
        'level' => 1,
        'name' => 'orange120',
    ),
    'arr4' =>
    array(
        'id' => 231,
        'level' => 1,
        'name' => 'black210',
    ),
    'arr5' =>
    array(
        'id' => 221,
        'level' => 3,
        'name' => 'red010',
    ),
);

I want to sort my second array according to the first array values. That means I need to sort my second array based on the first array values. My result array looks like this:

$a2 = Array
        (
            [arr5] => Array
                (
                    [id] => 221
                    [level] => 3
                    [name] => red010
                )
             [arr1] => Array
                (
                    [id] => 128
                    [level] => 1
                    [name] => blue210
                )
             [arr2] => Array
                (
                    [id] => 220
                    [level] => 2
                    [name] => green101
                )
              [arr4] => Array
                (
                    [id] => 231
                    [level] => 1
                    [name] => black210
                )
              
              [arr3] => Array
                (
                    [id] => 124
                    [level] => 1
                    [name] => orange120
                )
    )

Is it possible to sort it like this way? One is a single-dimensional array. The second array is the associative array.

Remy
  • 777
  • 2
  • 8
  • 15
  • Use `uasort()` with a comparison function that compares the indexes of the names in `$a1`. – Barmar Jul 19 '21 at 18:09
  • See the section **Sorting into a manual, static order** in the first linked question. – Barmar Jul 19 '21 at 18:10
  • It's not working for me. uasort() works with a single array. I have 2 arrays. I need to order my second array based on the first array values. –  Jul 19 '21 at 18:29
  • You use the first array in the callback function that sorts the second array. – Barmar Jul 19 '21 at 18:29
  • You're only sorting one array. – Barmar Jul 19 '21 at 18:29
  • Did you look at the example code I referred to? – Barmar Jul 19 '21 at 18:30
  • `return array_search($a['name'], $a1) - array_search($b['name'], $a1);` – Barmar Jul 19 '21 at 18:30
  • Yes. I tried with your code. But it doesn't work for me. Because in my first array the column name only contains values. The second array contains column names with values. And I want to sorry my second array values with the first array values. –  Jul 19 '21 at 18:52
  • What do you mean "the column name only contains values"? There are no column names, it's a 1-dimensional array. – Barmar Jul 19 '21 at 18:55
  • `uasort($a2, function($a, $b) use ($a1) { return array_search($a['name'], $a1) - array_search($b['name'], $a1); }` – Barmar Jul 19 '21 at 18:56
  • Yes. that's the same thing I told you. The a1 is a single-dimensional array. –  Jul 19 '21 at 18:58
  • So what's the problem? `array_search($a['name'], $a1)` expects `$a1` to be a 1-dimensional array. – Barmar Jul 19 '21 at 18:59
  • I used this code. But I didnt get any result –  Jul 19 '21 at 19:21
  • Works for me: https://ideone.com/DlMvDo – Barmar Jul 19 '21 at 19:26
  • I don't know. I used the exact code. But dint work for me. –  Jul 19 '21 at 19:27
  • I was missing `);` at the end of my comment above, did you fix that when you tried? – Barmar Jul 19 '21 at 19:30
  • Yes. I already fix that issue. –  Jul 19 '21 at 19:30
  • I don't know what to tell you. This is simple code, it should work for you the same as it does for me. – Barmar Jul 19 '21 at 19:32
  • Yes. Now it's working for me. I assign a new array to the result. That's Y I didn't get the result. Now I use to print the second array. SO I got the correct answer –  Jul 19 '21 at 19:33

0 Answers0