I currently have an array of objects as shown below:
$aa = new StdClass;
$aa->name = 'aa';
$aa->index = 30;
$bb = new StdClass;
$bb->name = 'bb';
$bb->index = 30;
$cc = new StdClass;
$cc->name = 'cc';
$cc->index = 10;
$dd = new StdClass;
$dd->name = 'dd';
$dd->index = 20;
$ee = new StdClass;
$ee->name = 'ee';
$ee->index = 10;
I have placed my objects into an array for sorting:
$arr = [$aa, $bb, $cc, $dd];
I also have an additional array which I would like to be used for sorting the objects in order at the index e.g. $aa->index
:
$map = [30, 20, 10, 10, 30];
An expected result would be:
[$aa, $dd, $cc, $ee, $bb]
The other of $aa
and $bb
is undefined, they must simply not repeat and all indices must be assigned somewhere.
What would be the best way to arrange $arr
in order set by $map
WITHOUT the use of a foreach()
loop?