doutu6658
2015-11-19 23:44
浏览 69
已采纳

如何在循环之前按键值对json字符串进行排序?

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 />
<?

}

?>

图片转代码服务由CSDN问答提供 功能建议

我想在循环之前按itemCategory_title或itemCategory_id排序这种json字符串? 我试图使用usort但它没有排序!

任何人都可以告诉我如何做到这一点?

json字符串示例数据:

  {'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代码: < pre> $ code2 = stripslashes($ _ POST ['outputtext']); $ clean_str = str_replace(“p'”,“'”,$ code2); $ clean_str = str_replace('\ '',''',$ clean_str); $ data = json_decode($ clean_str,true); //这里我想按itemCategory_title或itemCategory_id nortort对$ data进行排序($ data ,function($ a,$ b){//使用用户定义的函数对数组进行排序 返回$ a-&gt; itemCategory_id&gt; $ b-&gt; itemCatego ry_id? -1:1; //比较得分 }); 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']; ?&gt; &lt; a href =“./ process.php?tmp =&lt;?php echo $ item ['tmp'];?&gt;&amp; name =&lt;?php echo $ item [ 'name'];?&gt;&amp; abc =&lt;?php echo $ item ['abc'];?&gt;&amp; itemCategory_title =&lt;?php echo $ item ['itemCategory_title'];?&gt;&amp; log =&lt;?php echo $ item ['log'];?&gt;&amp; itemCategory_id =&lt;?php echo $ item ['itemCategory_id'];?&gt;“&gt;&lt;?php echo $ item [' itemCategory_title']; ?&GT; &LT; / A&GT; &lt; br /&gt; &lt;? } ?&gt;

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dtz46697 2015-11-19 23:53
    已采纳

    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.

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题