Ido Naveh
Ido Naveh

Reputation: 2492

Sort array elements based on another array's keys

In my PHP file I have 2 arrays, in each one the keys are numbered from 0 to its last index, and they both contains the same array elements number, as they are containing data on the same contact, but each array contains a different data about the same contact, and each contact has an ID which is his index on the array.

I have sorted the first array descending according to the values, so the keys are in different sort, and the values are descending. I want to sort the second array, in the same way, so they would have the same keys order, and then to do array_values on both of the arrays, in order it to have new ascending keys order.

For example I have these 2 arrays:

$arr1 = array('0' => 'John', '1' => 'George', '2' => 'James', '3' => 'Harry');
$arr2 = array('0' => '12', '1' => '8', '2' => '34', '3' => '23');

I have sorted $arr2 descending according to his values like this:

arsort($arr2);
// So now, $arr2 is "[2] => '34', [3] => '23', [0] => '12', [1] => '8'"

I want to sort $arr1 in the same way so it will also be:

$arr1 = array('2' => '34', [3] => '23', [0] => '12', [1] => '8');

How can I do this so the arrays will be sorted in the same keys order?

Upvotes: 2

Views: 3734

Answers (3)

BeetleJuice
BeetleJuice

Reputation: 40896

How about using array_multi_sort()? It rearranges all arrays to match the order of the first sorted array.

// as elements of $arr2 are shifted, corresponding elements of $arr1 will have the same shift
array_multisort($arr2, SORT_DESC, $arr1);

Live demo

Upvotes: 1

Asfandyar Khan
Asfandyar Khan

Reputation: 1738

So you have two arrays, so why not just loop over the second array and use its as a key order to create a new array using the the values from the 1st array..

$newArray = array();

foreach (array_keys($arr2) as $key) {
    $newArray[$key] = $arr1[$key];
}
// then apply sorting to new array
arsort($newArray);

Then simply print your new array $newArray to check your result.

print_r($newArray) or var_dump($newArray)

Expected output will be:

array(4) {
  [0]=>
  string(4) "John"
  [2]=>
  string(5) "James"
  [3]=>
  string(5) "Harry"
  [1]=>
  string(6) "George"
}

Similarly if you want an opposite, Then change just replace $arr2 with $arr1 like wise.

$newArray = array();

foreach (array_keys($arr1) as $key) {
    $newArray[$key] = $arr2[$key];
}
// then apply sorting to new array
arsort($newArray);
var_dump($newArray)`

Expected output will be:

array(4) {
  [2]=>
  string(2) "34"
  [3]=>
  string(2) "23"
  [0]=>
  string(2) "12"
  [1]=>
  string(1) "8"
}

Upvotes: -1

ishegg
ishegg

Reputation: 9927

How about this? Using array_replace():

<?php
$arr1 = array('0' => 'John', '1' => 'George', '2' => 'James', '3' => 'Harry');
$arr2 = array('0' => '12', '1' => '8', '2' => '34', '3' => '23');
arsort($arr2);
var_dump(array_replace($arr2, $arr1)); // array(4) { [2]=> string(5) "James" [3]=> string(5) "Harry" [0]=> string(4) "John" [1]=> string(6) "George" }

Demo

Upvotes: 3

Related Questions