1

I have two arrays

// array of objects
$records = array(
  [0] => (object) [
             'id' => 1, //  (*)
             ....
        ],
  [1] => (object) [
            'id' => 2, //  (*)
             ....
        ],
   [2] => (object) [
            'id' => 3, //  (*)
             ....
        ],
);

// array 2
// the keys in this array refer to the object ids (*)
$sorted = array(
    '2'  => 7,
    '3'  => 4,
    '1'  => 2,
);

$new_records = array();

What I want to do is to sort the values of first array (i.e the objects) based on the order of the key index of the second array, so the end result in this case will become:

 $new_records = array(
      [0] => (object) [
                 'id' => 2,
                 ....
            ],
      [1] => (object) [
                'id' => 3,
                 ....
            ],
       [2] => (object) [
                'id' => 1,
                 ....
            ],
    );

$records = $new_records;
ltdev
  • 4,037
  • 20
  • 69
  • 129

4 Answers4

0

Code: (Demo)

$records = [(object)['id' => 1], (object)['id' => 2], (object)['id' => 3]];
$sorted = ['2' => 7, '3' => 4, '1' => 2];

$tempArr = array();
foreach ($records as $value) {
     $tempArr[$value->id] = $value;
}
$resultArr = array_values(array_replace($sorted, $tempArr));

var_export($resultArr);

Output:

array (
  0 => 
  stdClass::__set_state(array(
     'id' => 2,
  )),
  1 => 
  stdClass::__set_state(array(
     'id' => 3,
  )),
  2 => 
  stdClass::__set_state(array(
     'id' => 1,
  )),
)
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
Quang Minh
  • 116
  • 2
  • I have tidied up your code block and removed the useless "Try this" and "Good luck" texts that have no place in any answer. Please honor StackOverflow by adding an explanation to this post that will educate the OP and future researchers. – mickmackusa Apr 21 '18 at 05:51
0

Try this

$new_records = array();
foreach( $sort as $id => $pos ) {
    foreach( $records as $record ) {
        if( $record[ 'id' ] == $id ) {
            $new_records[] = $record;
            break;
        }
    }
}
A. Blub
  • 792
  • 1
  • 5
  • 10
  • Your code works. But I think your code have a performance problem with a nested for loop. Try my code with 1 for and 1 function array_replace. – Quang Minh Jun 14 '17 at 13:07
0

This make the job

$records = array(
   0 => (object) [
             'id' => 1, //  (*)
        ],
   1 => (object) [
            'id' => 2, //  (*)
        ],
   2 => (object) [
            'id' => 3, //  (*)
        ],
);

// array 2
// the keys in this array refer to the object ids (*)
$sorted = array(
    '2'  => 7,
    '3'  => 4,
    '1'  => 2,
);

$keySorted = array_keys($sorted);

usort($records, function ($a, $b) use ($keySorted) {

    $pos_a = array_search($a->id, $keySorted);
    $pos_b = array_search($b->id, $keySorted);
    return $pos_a - $pos_b;
});

var_dump($records);
fxlacroix
  • 557
  • 4
  • 18
  • This is a good / direct approach. Please improve your answer by explaining how your code block works. Remember, not every developer is familiar with the syntax of `usort()` and anonymous functions with a `use` argument. Explain what kinds of values are generated from `array_search()` including the flow-on effect of a `false` return value being used in the subtraction line. Always, always, always explain every answer that you post. Thank you for caring for StackOverflow. – mickmackusa Apr 21 '18 at 05:59
  • ...very similar to [this post from 5 years earlier](https://stackoverflow.com/a/11145568/2943403). – mickmackusa Apr 22 '18 at 11:47
0

Here, I have created sorting of array and add to your array of object using current() and key() is from php.

$i = 0;
while ($value = current($sorted)) {

    $key = key($sorted); // get key from array 2
    $records[i]->array('id' => $key); // set key to array of object

    next($array);
    $i++;
}

I hope this may help you.

  • This answer does not work. Proof: https://3v4l.org/TOGt1 It has typos, but even after correcting them, the new record declaration damages the input data that needs to be retained. Please fix or delete this answer. – mickmackusa Apr 21 '18 at 06:04