1

I have three arrays that are synced with the same key and I need to natsort and apply array_values() to array1 while keeping array2 and array3 in sync with the new keys set for array1 by array_values().

I need to sort the first array naturally and have the other arrays' elements moved to new positions so that columns remain related.

Input arrays:

$array1 = ['OA.3', 'OA.8', 'OA.6', 'OA.2'];
$array2 = [4, 1, 5, 3];
$array3 = [3, 1, 5, 0];

Desired result:

$array1 = ['OA.2', 'OA.3', 'OA.6', 'OA.8'];
$array2 = [3, 4, 5, 1];
$array3 = [0, 3, 5, 1];

Is there some way to keep all three of these arrays in sync during the natsort() and array_values() sorting of $array1? The final result of array2 and array3 show the new keys matching the final result of array1's sorting and re-keying.

6
  • what do you want as final output? I am having problems understanding your question. Commented Jul 25, 2015 at 21:04
  • @Viral Sorry I just updated the OP. realized it was a bit confusing after re-reading the post. Let me know if it's better Commented Jul 25, 2015 at 21:05
  • Yes, better now, also post array1 , 2 and 3 as a result of var_export(), not print_r() Commented Jul 25, 2015 at 21:07
  • @Viral ok just changed the output to var_export() for you also Commented Jul 25, 2015 at 21:11
  • I feel like i need to do this manually and ready the order of natsort with the keys somehow and store them in temp variables to apply them to the other 2 arrays but i cant even get my head around it. and im not the greatest with php so i wouldnt even know if its possible to read the order of natsort and store those numbers and their actual keys in a way to apply them without destroying the other 2 arrays Commented Jul 25, 2015 at 21:24

3 Answers 3

2

You can use the keys of array A after the natsort, to sort the array B and C... Example:

<?PHP
function pre($a) { echo '<pre>'; print_r( $a ); echo '</pre>'; }
$a = array(
  0 => 'OA.3',
  1 => 'OA.8',
  2 => 'OA.6',
  3 => 'OA.2'
);
$b = array (
  0 => '4',
  1 => '1',
  2 => '5',
  3 => '3'
);
$c = array (
  0 => 3,
  1 => 1,
  2 => 5,
  3 => 0,
);
pre( $a );
natsort( $a );
pre( $a );
foreach( $a AS $key => $var ) {
    $bb[] = $b[ $key ];
    $cc[] = $c[ $key ];
}
echo '$bb:<br />';
pre( $bb );
echo '$cc:<br />';
pre( $cc );
?>

You can apply array_values() on array $a, $bb and $cc after the sorting is done.

Note: pre is just to print the array's inbetween and has no other function.

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

Comments

0

Here is my solution, you can do the same for $required_array3

$required_array2 = array();

foreach ($your_array2 as $key => $value)
{
    $k = array_search($value, $your_array1); // Original array [unsorted]
    $required_array2[$k] = $value;
}

Comments

0

This task is exactly what array_multisort() is designed to do. Write the first array as parameter one, then add parameters to dictate the direction and comparison style, then add the other arrays to be synchronously mutated. Demo

$array1 = ['OA.3', 'OA.8', 'OA.6', 'OA.2'];
$array2 = [4, 1, 5, 3];
$array3 = [3, 1, 5, 0];

array_multisort($array1, SORT_ASC, SORT_NATURAL, $array2, $array3);

var_dump($array1, $array2, $array3);

Output:

array(4) {
  [0]=>
  string(4) "OA.2"
  [1]=>
  string(4) "OA.3"
  [2]=>
  string(4) "OA.6"
  [3]=>
  string(4) "OA.8"
}
array(4) {
  [0]=>
  int(3)
  [1]=>
  int(4)
  [2]=>
  int(5)
  [3]=>
  int(1)
}
array(4) {
  [0]=>
  int(0)
  [1]=>
  int(3)
  [2]=>
  int(5)
  [3]=>
  int(1)
}

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.