3

I have an array with values as follows:-

$Array1 = array("myfirst_value", "mysecond_value", "mythird_value"}

Now for my another array, the listing comes in randomly as follows:-

$Array2 = array
(
    [4] => myfirst_value
    [8] => myforth_value
    [21] => mysecond_value
    [7] => myfifth_value
    [17] => mysixth_value
    [20] => mythird_value
    [16] => myseventh_value
)

What I am hoping to achieve is that the $Array2 gets sorted based on the order of values in $Array1.

So, I am hoping that $Array2 gets sorted and becomes:-

$Array2 = array
(
        [4] => myfirst_value
        [21] => mysecond_value
        [20] => mythird_value


        [7] => myfifth_value
        [8] => myforth_value
        [16] => myseventh_value
        [17] => mysixth_value

)

Notice, the values of Array1 are sorted first and rest are just outputted without any order.

Thanks

2

3 Answers 3

1

If i understood correctly

$Array1 = array("myfirst_value", "mysecond_value", "mythird_value");

$Array2 = array
(
    4 => myfirst_value,
    8 => myforth_value,
    21 => mysecond_value,
    7 => myfifth_value,
    17 => mysixth_value,
    20 => mythird_value,
    16 => myseventh_value
);

// Remove elements of the 1st array from the 2nd
function f ($v) { global $Array1;  return in_array($v, $Array1);}
$a1 = array_filter($Array2, 'f');    
// Take the rest elements
$a2 = array_diff_key($Array2, $a1);
// Combine back 
print_r($a1+$a2);

result

Array
(
    [4] => myfirst_value
    [21] => mysecond_value
    [20] => mythird_value
    [8] => myforth_value
    [7] => myfifth_value
    [17] => mysixth_value
    [16] => myseventh_value
)
Sign up to request clarification or add additional context in comments.

5 Comments

thanks a lot for taking out time to do this. Unfortunately, this is giving me white screen. I guess it's not supported in my php version.
What version do you use?
@Steve Try now - i remove the anonymous function
Thanks a lot splash. Unfortunately, it's not doing anything now. $a1 is basically coming out empty
eval.in/380070 there it works. I look later, what else may does not work in your version
1

You can also use uasort() function as

uasort($Array2, function($a,$b) use (&$Array1){
    foreach($Array1 as $key => $value){
        if($a == $value){
            return 0;
            break;
        }
        if($b == $value){
            return 1;
            break;
        }
    }
    if($a == $b){ return 0;}  return ($a < $b) ? -1 : 1;
});

Fiddle

Comments

0

Apart from splash's perfect answer, this also works:-

 $Sorted_Array = array_merge(
        array_intersect($Array1, $Array2), 
        array_diff($Array2, $Array1)
 );

3 Comments

array_intersect($Array1, $Array2) - looses the keys. It is wrong answer :)
:-D ... now ofcourse I can't even compare to your answer :)
I'm serious - result of function will have keys 0,1,2... But You want to save them from source array

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.