-1

I have the following scenario

$elementsInPairs = ["xyz","xxx","yyy","zzz"];
$valuesInPair =[4,2,3,1];

and i need to sort the second array which would give me

[1,2,3,4]

which i can do by

sort($valuesInPair)

but i need the same to happen in first array as well, based on the sorting of the second array

["zzz","xxx","yyy","xyz"];

EDIT As i had to fix it urgently, i guess i wasn't able to share more information, and the question seemed vague

basically the idea is that i have two sets of arrays, same number of elements in all cases, and they are linked to each other, The second array is a set of order IDs. and first array is set of names, so in the above example, i have

4 relates to xyz
2 relates to xxx
3 relates to yyy
1 relates to zzz

So i need to sort based on IDs, and have the same order reflect in the first array as well,

so the final result would be,

["zzz","xxx","yyy","xyz"];

which is sorted based on

[1, 2, 3, 4];

Hopefully this clears things above

2
  • 3
    Show us what you tried, please. This is not a code-writing service, you need to make an attempt yourself. Commented Jun 23, 2020 at 10:02
  • Do you mean ["zzz","yyy","xxx","xyz"]; so like reversing the array? Because I dont see a pattern in your "order" for the array with letters. xxx and yyy comes before zzz if you order by alphabetic, so what is the idea behind the ordering? Commented Jun 23, 2020 at 10:17

5 Answers 5

1

You can achieve this by using array_multisort method.

array_multisort($valuesInPair, SORT_ASC, $elementsInPair);
Sign up to request clarification or add additional context in comments.

Comments

0

Use this below code, this does exactly what you're looking for.

$elementsInPairs = ["xyz","xxx","yyy","zzz"];
$valuesInPair =[4,2,3,1];

$data = array_combine($elementsInPairs,$valuesInPair);
asort($data);

$dumpdata = [];

foreach($data as $x => $x_value) {
    $dumpdata[] = $x;
}

print_r($dumpdata);

I hope this helps you.

1 Comment

How did you know that this does exactly what the OP is looking for?
0

You kan do like this :

<?php

$elementsInPairs = ["xyz","xxx","yyy","zzz"];
$valuesInPair =[4,2,3,1];
//use [asort][1] - Sort an array in reverse order and maintain index association
asort($valuesInPair);
// and make a new array to sort elementsInPairs
$newelementsInPairs = array();
foreach($valuesInPair as $key=>$val){
    $newelementsInPairs[] = $elementsInPairs[$key];
}
print_r(implode(",",$valuesInPair)."\n");
print_r(implode(",",$newelementsInPairs));
/** Output

1,2,3,4
zzz,xxx,yyy,xyz

 **/

Comments

0

Hi please combine two array and sort

$newArray =array_combine($valuesInPair,$elementsInPairs);

then sort($newArray);

1 Comment

This would produce a different array
0

You can use array_combine(), ksort() and array_values():

<?php

$elementsInPairs = ["xyz","xxx","yyy","zzz"];
$valuesInPair = [4,2,3,1];

$newArray = array_combine($valuesInPair, $elementsInPairs);
ksort($newArray);
$sortedElements = array_values($newArray);

print_r($sortedElements);

will output

Array
(
    [0] => zzz
    [1] => xxx
    [2] => yyy
    [3] => xyz
)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.