2

I would like to sort this kind of json string by itemCategory_title or itemCategory_id before the loop? i tried to use usort but it didn't sort!

could any one tell me how this can be done ?

json string sample data:

{ '5123': {
            'tmp': '1', 'name': 'mango', 'abc': 'abcd4 http://mysite/items/1234', 'number': '1123', 'itemCategory_title': 'fruits', 'logo': '2123.png', 'itemCategory_id': '90'
        }, '700': {
            'tmp': '0', 'name': 'cherry', 'abc': 'abcd4 http://mysite/items/1235', 'number': '1124', 'itemCategory_title': 'fruits', 'logo': '2124.png', 'itemCategory_id': '91'
        } }

php code:

$code2 = stripslashes($_POST['outputtext']);

$clean_str = str_replace("p '","'",$code2);
$clean_str = str_replace('\'', '"', $clean_str);

$data = json_decode($clean_str, true);

//here i want to sort $data by itemCategory_title or itemCategory_id

usort($data, function($a, $b) { //Sort the array using a user defined function
    return $a->itemCategory_id > $b->itemCategory_id ? -1 : 1; //Compare the scores
}); 

foreach( $data as $item ) {
  echo $item['tmp'];
  echo $item['name'];
  echo $item['abc'];
  echo $item['number'];
  echo $item['itemCategory_title'];
  echo $item['log'];
  echo $item['itemCategory_id'];    

?>
<a href="./process.php?tmp=<?php  echo $item['tmp'] ; ?>&name=<?php  echo $item['name']; ?>&abc=<?php  echo $item['abc'] ; ?>&itemCategory_title=<?php  echo $item['itemCategory_title'] ; ?>&log=<?php  echo $item['log'] ; ?>&itemCategory_id=<?php  echo $item['itemCategory_id'] ; ?>"><?php  echo $item['itemCategory_title'] ; ?> </a> <br />
<?

}

?>
5
  • Can i ask you why you want to sort a string representation of an object? Commented Nov 19, 2015 at 23:48
  • @ThisNameBetterBeAvailable He's sorting it after he decodes it into an associative array. Commented Nov 19, 2015 at 23:49
  • @Barmar but why is he sorting it (don't say object nor array)? Commented Nov 19, 2015 at 23:53
  • He wants the HTML to show them in the order of itemCategory_id. Commented Nov 19, 2015 at 23:54
  • Why don't you print_r($data) after your json_decode? You'll see your problem. Commented Nov 19, 2015 at 23:55

1 Answer 1

3

In the usort function, ->itemCategory_id should be ['itemCategory_id']. Since you gave the second argument to json_decode(), the JSON objects become PHP associative arrays, not objects.

If you'd turned on error reporting you would have seen the notices about trying to get the properties of a non-object. You got it right when you were echoing them in the foreach loop, but not the usort comparison function.

usort($data, function($a, $b) { //Sort the array using a user defined function
    return $a['itemCategory_id'] > $b['itemCategory_id'] ? -1 : 1; //Compare the scores
}); 

To sort by name within categories, it should be:

usort($data, function($a, $b) { //Sort the array using a user defined function
  if ($a['itemCategory_id'] == $b['itemCategory_id']) {
    return $a['name'] > $b['name'] ? 1 : -1;
  } else {
    return $b['itemCategory_id'] - $a['itemCategory_id'];
  }
}); 

This sorts category IDs descending, and names ascending.

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

13 Comments

Note that the ternary operation is not mandatory, just use return $a['itemCategory_id'] - $b['itemCategory_id'] for ASC order and switch $a and $b for DESC order.
@ChristopheWillemsen That works in this case because they're numbers, it wouldn't work in the more general case. And it's not really relevant to the problem in the code.
Thanks all for replies.Barmar it sorted correctly now. If i want to sort all items that got same itemCategory_id alphabetically do i need to push those items with same itemCategory_id to another array then sort ? or is there any easier method ?
You add a check for that to the usort function. if ($a['itemCategory_id'] == $b['itemCategory_id']) { return $a['name'] > $b['name'] ? -1 : 1; } else { ... }`
i tried that but didn't sort it the way i want it. This is the way i want my final data be sorted:apple (fruits=>itemCategory_id=12) bannana(fruits=>itemCategory_id=12) bmw(cars=>itemCategory_id=11) ford(cars=>itemCategory_id=11) i want all items that have same itemcategory for example id=11 be sort a-z not mixed or z-a
|

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.