Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have an array which holds around 5000 array elements, each in the following format:

Array
        (
            [keywordid] => 98
            [keyword] => sample keyword 34
            [type] => NATURAL
            [longname] => UK
        )

I have a second array which holds numerical values such as the following:

Array
(
    [0] => 55
    [1] => 56
    [2] => 57
    [3] => 58
    [4] => 59
    [5] => 1065
    [6] => 1066
    [7] => 1067
    [8] => 1083
)

Each value in the array above corresponds to the 'keywordid' value within each array of the first array. I want to sort the first array, so that those arrays whose keywordid has a value matching an element in the second array, appear first and the rest of the arrays appear afterwards in no specified order. How do I accomplish this? I am using PHP 5.3, backwards compatibility is not a requirement.

Appreciate the help.

share|improve this question
    
What is the expected behaviour if you have duplicates in the second array? [ 55, 56, 57, 55 ] –  adlawson Sep 25 '13 at 11:23
    
There won't be duplicates in the second array. –  Sid Sep 25 '13 at 11:27

2 Answers 2

up vote 2 down vote accepted

I would probably use usort

usort($array1, function($a, $b) use($array2) {
    $k1 = array_search($a['keywordid'], $array2);
    $k2 = array_search($b['keywordid'], $array2);

    if ($k1 == $k2) {
        return 0;
    }
    return ($k1 < $k2) ? -1 : 1;
});

There is probably a better way but that came to mind first.

share|improve this answer
    
I changed $k1 < $k2 to $k1 > $k2 otherwise they appear last. Editing the answer wouldn't let me edit just one character. –  Sid Sep 25 '13 at 11:52
    
Discovered that not all the expected elements get sorted to the top, one is always missed out. –  Sid Sep 26 '13 at 9:33

try this code to manipulate that array:

<?php
//here I assume you have more than one array
$array = array (
            0=> array (
            "keywordid" => 98,
            "keyword" => "sample keyword 34",
            "type" => "NATURAL",
            "longname" => "UK"),
            1=> array (
            "keywordid" => 95,
            "keyword" => "sample keyword 95",
            "type" => "NATURAL 02",
            "longname" => "US"),
            2=> array (
            "keywordid" => 55,
            "keyword" => "sample keyword 55",
            "type" => "NATURAL 02",
            "longname" => "AU")

        );

//populate array into new variable
foreach ( $array  as $key=> $val){
    $out[] = $val["keywordid"];
}

echo "<pre>";
print_r($out);
echo "</pre>";  

?>

the Output is:

Array
(
    [0] => 98
    [1] => 95
    [2] => 55
)
share|improve this answer
1  
All you are doing is copying keyword ids to another array. You aren't sorting anything. –  Rob Sep 25 '13 at 11:38

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.