3

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?

6
  • php.net/manual/en/function.array-multisort.php Commented Aug 31, 2017 at 17:14
  • 2
    Why is this related data in two separate arrays? Commented Aug 31, 2017 at 17:17
  • It's somewhat unclear what your desired result is. You say that you want $arr1 = array('2' => '34', [3] => '23', [0] => '12', [1] => '8');, but I don't think that's actually the case Commented Aug 31, 2017 at 17:24
  • @PatrickQ I think he meant array(4) { [2]=> string(5) "James" [3]=> string(5) "Harry" [0]=> string(4) "John" [1]=> string(6) "George" }, he didn't change the values. Commented Aug 31, 2017 at 17:26
  • @ishegg I think so too, but I'd rather not assume/guess Commented Aug 31, 2017 at 17:28

3 Answers 3

3

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

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

-1

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"
}

Comments

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.