0

Im trying to sort an multidimentional array based on a array in the order i would like it to appear in.

the $lookingfor array contains the order i would like it to appear, while $avaliableArray contains the whole multidimentional array. Would like the result in the example code called $resultarray

How would i do this, based on the id of the elements in $avaliableArray ?

code:

$avaliableArray = array(       
         array('name' => 'Banken', 'site' => 'bank', 'avaliable' => true, 'type' => 'site', 'id' => "testid-1"),
                array('name' => 'Banken', 'site' => 'bank', 'avaliable' => true, 'type' => 'site', 'id' => "testid-4"),

                array('name' => 'Banken', 'site' => 'bank', 'avaliable' => true, 'type' => 'site', 'id' => "testid-8")
);


$lookingFor = array(1,8,4);

looking for result:

$resultArray = array(       
         array('name' => 'Banken', 'site' => 'bank', 'avaliable' => true, 'type' => 'site', 'id' => "testid-1"),
                array('name' => 'Banken', 'site' => 'bank', 'avaliable' => true, 'type' => 'site', 'id' => "testid-8"),

                array('name' => 'Banken', 'site' => 'bank', 'avaliable' => true, 'type' => 'site', 'id' => "testid-4")
);
maria
  • 207
  • 5
  • 22
  • 56
  • 2
    you can use `array_multisort` or `usort`.... There are lots of question like this. – Murad Hasan May 31 '16 at 10:43
  • It could be 1-8,2 so order by that i think wont work. – maria May 31 '16 at 10:44
  • 1
    [one](http://stackoverflow.com/questions/2699086/sort-multi-dimensional-array-by-value), [two](http://stackoverflow.com/questions/96759/how-do-i-sort-a-multidimensional-array-in-php) and so on... – Murad Hasan May 31 '16 at 10:44
  • Why is this question downvoted? The questions linked by @FrayneKonok are not similar (`$lookingFor` is the significant thing here) – Chris Lear May 31 '16 at 10:52
  • @ChrisLear The first portion is same `testid-*` so why not this work with what i linked??? – Murad Hasan May 31 '16 at 10:57
  • @ChrisLear, Why you blame me?? Do you think here is my fault??? – Murad Hasan May 31 '16 at 10:58
  • @FrayneKonok it seems to me that you haven't seen the significance of the `$lookingFor` array. The two non-accepted answers also miss this. – Chris Lear May 31 '16 at 11:05
  • @ChrisLear, What `usort` did? do your know? – Murad Hasan May 31 '16 at 11:22
  • @FrayneKonok I don't understand your question. But I know that the accepted answer solves the OP's problem using `usort`, taking some liberties with the original code, but getting the logic right. The essential thing is that it uses `$lookingFor` within the `usort` callback. – Chris Lear May 31 '16 at 11:28

3 Answers3

1

Use usort() and apply proper id to $lookingFor same as array value.

$avaliableArray = array(       
         array('name' => 'Banken', 'site' => 'bank', 'avaliable' => true, 'type' => 'site', 'id' => "testid-1"),
                array('name' => 'Banken', 'site' => 'bank', 'avaliable' => true, 'type' => 'site', 'id' => "testid-4"),

                array('name' => 'Banken', 'site' => 'bank', 'avaliable' => true, 'type' => 'site', 'id' => "testid-8")
);


$lookingFor = array("testid-1","testid-8","testid-4");
usort($avaliableArray, function ($a, $b) use ($lookingFor) {
    $pos_a = array_search($a['id'], $lookingFor);
    $pos_b = array_search($b['id'], $lookingFor);
    return $pos_a - $pos_b;
});
echo "<pre>";
print_r($avaliableArray);

Output

Array
(
    [0] => Array
        (
            [name] => Banken
            [site] => bank
            [avaliable] => 1
            [type] => site
            [id] => testid-1
        )

    [1] => Array
        (
            [name] => Banken
            [site] => bank
            [avaliable] => 1
            [type] => site
            [id] => testid-8
        )

    [2] => Array
        (
            [name] => Banken
            [site] => bank
            [avaliable] => 1
            [type] => site
            [id] => testid-4
        )

)
RJParikh
  • 4,096
  • 1
  • 19
  • 36
0

You should try usort :

function sortIt($a, $b) {
    return strcasecmp($a['id'],$b['id']);
}

usort($resultArray, 'sortIt');
chandresh_cool
  • 11,753
  • 3
  • 30
  • 45
0

Try this:

function sortArr($a, $b) {
    return intval(preg_replace('/[^0-9]+/', '', $a['id']), 10) - intval(preg_replace('/[^0-9]+/', '', $b['id']), 10) ; // it will extract number from string and compare it
}

usort($avaliableArray, 'sortArr');
Jayesh Chitroda
  • 4,987
  • 13
  • 18