11

I have two arrays, both have the same keys (different values) however array #2 is in a different order. I want to be able to resort the second array so it is in the same order as the first array.

Is there a function that can quickly do this?

0

5 Answers 5

13

I can't think of any off the top of my head, but if the keys are the same across both arrays then why not just loop over the first one and use its key order to create a new array using the the values from the 2nd one?

$arr1 = array(
    'a' => '42', 
    'b' => '551',
    'c' => '512',
    'd' => 'gge',
) ;


$arr2 = array(
    'd' => 'ordered',
    'b' => 'is',
    'c' => 'now',
    'a' => 'this', 
) ;

$arr2ordered = array() ;

foreach (array_keys($arr1) as $key) {
    $arr2ordered[$key] = $arr2[$key] ;
}
Sign up to request clarification or add additional context in comments.

Comments

6

You can use array_replace

$arr1 = [
    'x' => '42', 
    'y' => '551',
    'a' => '512',
    'b' => 'gge',
];


$arr2 = [
    'a' => 'ordered',
    'x' => 'this',
    'y' => 'is',
    'b' => 'now',
];

$arr2 = array_replace($arr1, $arr2);

$arr2 is now

[
    'x' => this,
    'y' => is,
    'a' => ordered,
    'b' => now,

]

4 Comments

I upvoted it, but, maybe, for nothing, because result is the following: array ( 'd' => 'ordered', 'b' => 'is', 'c' => 'now', 'a' => 'this', )
Yes, the right is array_multisort(array_keys($arr1), SORT_DESC, SORT_STRING, $arr2);
No again! Result is array ( 'a' => 'this', 'c' => 'now', 'b' => 'is', 'd' => 'ordered', )! I think, you don't test your answers.
Yes, I'm sorry, will be better to use array_replace, now it is right, i hope.
0
foreach(array_keys($array1) as $key)
{
  $tempArray[$key] = $array2[$key];
}
$array2 = $tempArray;

Comments

0

I am not completely sure if this is what your after. anyways as long as the the array remains the same size, than this should work for you.

$gamey = array ("wow" => "World of Warcraft", "gw2" => "Guild Wars2", "wiz101" => "Wizard 101");
$gamex = array ("gw2" => "best game", "wiz101" => "WTF?", "wow" => "World greatest");


function match_arrayKeys ($x, $y)
{
    $keys    = array_keys ($x);
    $values  = array_values ($y);

    for ($x = 0; $x < count ($keys); $x++)
    {
        $newarray [$keys[$x]] = $y[$keys[$x]];
    }
    return $newarray;
}

print_r (match_arrayKeys ($gamey, $gamex)); 

Output

[wow] => World greatest
[gw2] => best game
[wiz101] => WTF?

Comments

-1

Try this
CODE

$fruits = array("d"=>"lemon", "a"=>"orange", "b"=>"banana", "c"=>"apple");
ksort($fruits);
foreach ($fruits as $key => $val) {
    echo "$key = $val\n";
}

OUTPUT

a = orange
b = banana
c = apple
d = lemon

Check the php manual for ksort()

3 Comments

Ksort just sorts the array by key, I was hoping for a function that can resort a 2nd array to match the first array.
how about using ksort() function to both arrays ? ?
using ksort() on both arrays will change order of both array, the question was to align the order of the 2nd array to the first one

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.