1

i have an Array Object like this :

[
    {'id' : 1, 'school' : 'Harvard', 'score' : 90},
    {'id' : 2, 'school' : 'LA University', 'score' : 50},
    {'id' : 4, 'school' : 'Cairo', 'score' : 90},
    {'id' : 3, 'school' : 'Monster University', 'score' : 70},
    {'id' : 4, 'school' : 'Cairo', 'score' : 30},
    {'id' : 1, 'school' : 'Harvard', 'score' : 80},
]

and my expected result is like this :

[
    {'id' : 1, 'school' : 'Harvard', 'score' : 90},
    {'id' : 2, 'school' : 'LA University', 'score' : 50},
    {'id' : 3, 'school' : 'Monster University', 'score' : 70},
    {'id' : 4, 'school' : 'Cairo', 'score' : 90},
]

i tried some function like array_unique and array_intersect but the result is different than i expected

so my purpose is get the higher score from the same record when the record have multiple record

hope you guys can help me

thanks

3 Answers3

1

You can easily do this by using Lodash

<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>
<script type="text/javascript">
var data = [
  { 'id': 1, 'school': 'Harvard', 'score': 90 },
  { 'id': 2, 'school': 'LA University', 'score': 50 },
  { 'id': 4, 'school': 'Cairo', 'score': 90 },
  { 'id': 3, 'school': 'Monster University', 'score': 70 },
  { 'id': 4, 'school': 'Cairo', 'score': 30 },
  { 'id': 1, 'school': 'Harvard', 'score': 80 },
]

var result = _(data)
  .orderBy(['id', 'school'], ['asc', 'desc'])
  .uniqBy('id')
  .value();

console.log(result);
</script>
Ahsan Ali
  • 4,951
  • 2
  • 17
  • 27
Nirali
  • 1,776
  • 2
  • 12
  • 23
1

There may be a better way to this, but this does work with your example data set. I adapted this from a Youtube tutorial by "TUTORIALS AND TRAINING" on call back functions. https://www.youtube.com/watch?v=6jrZhkphCng

This usort() method is also demonstrated in the link that @Sameera referenced.

usort($array, function($a, $b){

  if([$a['id'], $a['score']] < [$b['id'], $b['score']]){

    return -1;

  }elseif([$a['id'], $a['score']] > [$b['id'], $b['score']]){

      return 1;

    } else {

      return 0;

     }

});


for($i = 0; $i < count($array); $i++){

  if($array[$i]['id'] != $array[$i+1]['id']){

  $results[$i]['id']      = $array[$i]['id'];
  $results[$i]['school']  = $array[$i]['school'];

  }
}

array_reverse($results);

json_encode($results);  //<--This will output a json encoded string.

This will output an array like this:

     Array
(
    [1] => Array
        (
            [id] => 1
            [school] => Harvard
        )

    [2] => Array
        (
            [id] => 2
            [school] => LA University
        )

    [3] => Array
        (
            [id] => 3
            [school] => Monster University
        )

    [5] => Array
        (
            [id] => 4
            [school] => Cairo
        )

)

Or if you use the json_encode() a json encoded string like this:

{"1":{"id":1,"school":"Harvard"},"2":{"id":2,"school":"LA University"},"3":{"id":3,"school":"Monster University"},"5":{"id":4,"school":"Cairo"}}
Joseph_J
  • 3,654
  • 2
  • 13
  • 22
0
$data = 'Your array of objects';

usort($data, function($a, $b)
{
if($a->id == $b->id)
    return $b->score - $a->score;
return strcmp($a->id, $b->id);
});

$known = array();
$finalData = array_filter($data, function ($val) use (&$known) {
$unique = !in_array($val->id, $known);
$known[] = $val->id;
return $unique;
});

print_r($finalData); // your desired array