【问题标题】:json array sort by value in phpphp中的json数组按值排序
【发布时间】:2018-10-17 15:18:59
【问题描述】:

我有 JSON 对象数组。我正在尝试使用 usort 对数组进行排序。我想使用来自field_listing_ordervalue 字段。它使用值排序。我错过了一些东西,但无法弄清楚。请查看代码。谢谢!

stdClass Object
(
    [node_title] => abc
    [nid] => 2281
    [field_api_order_value] => 201
    [field_node_entity_type] => node
    [_data] => Array
        (
            [nid] => Array
                (
                    [entity_type] => node
                    [entity] => stdClass Object
                        (

                            [title] => abc
                            [field_listing_order] => Array
                                (
                                    [und] => Array
                                        (
                                            [0] => Array
                                                (
                                                    [value] => 8
                                                    [format] => 
                                                    [safe_value] => 8
                                                )

                                        )

                                )

                       )

                 )

        )

)

stdClass Object
(
    [node_title] => abc
    [nid] => 2243
    [field_api_order_value] => 204
    [field_node_entity_type] => node
    [_data] => Array
        (
            [nid] => Array
                (
                    [entity_type] => node
                    [entity] => stdClass Object
                        (

                            [title] => abc
                            [field_listing_order] => Array
                                (
                                    [und] => Array
                                        (
                                            [0] => Array
                                                (
                                                    [value] => 3
                                                    [format] => 
                                                    [safe_value] => 3
                                                )

                                        )

                                )

                       )

                 )

        )

) stdClass Object
(
    [node_title] => abc
    [nid] => 2431
    [field_api_order_value] => 242
    [field_node_entity_type] => node
    [_data] => Array
        (
            [nid] => Array
                (
                    [entity_type] => node
                    [entity] => stdClass Object
                        (

                            [title] => abc
                            [field_listing_order] => Array
                                (
                                    [und] => Array
                                        (
                                            [0] => Array
                                                (
                                                    [value] => 1
                                                    [format] => 
                                                    [safe_value] => 1
                                                )

                                        )

                                )

                       )

                 )

        )

)

等等……

  foreach ($view->result as $result) {   
        $node = $result->_data['nid']['entity'];  
        $listing_order = $node->field_listing_order[LANGUAGE_NONE][0];
         .....            
       // code goes here and it works well. sorting issue
}

 usort ($node->field_listing_order[LANGUAGE_NONE][0], function($a, $b){
       return strcmp($a->value, $b->value);
   }); ?>

【问题讨论】:

  • 您想对field_listing_order 或“全局”对象中的元素进行排序?
  • 这是关于使用不在内部的值对全局对象进行排序。 $a 和 $b 用于比较值。

标签: php arrays sorting multidimensional-array usort


【解决方案1】:

如果您需要使用field_listing_order 值对所有节点进行排序,则需要沿完整路径比较值,从第一个对象到值:

usort($view->result, function($a, $b) {
    $la = $a->_data['nid']['entity']->field_listing_order[LANGUAGE_NONE][0]['value'];
    $lb = $b->_data['nid']['entity']->field_listing_order[LANGUAGE_NONE][0]['value'];
    return $la - $lb ;
});

在这种情况下,$a$b 是可以比较的两个不同节点。这就是为什么你应该比较这些节点的field_listing_order 的值。答案是使用$la- $lb

【讨论】:

  • 注意:这种排序当然应该在循环之外进行。
  • 它没有排序,也没有看到任何错误代码。如果数组已经排序怎么办。我们仍然可以使用其他元素进行排序。对吗?
  • 是的,如果数组已经排序,则没有任何变化。您可以尝试$la-$lb 来检查订单是否颠倒。是你的问题吗?
  • 我只是在问。我会试试$la-$lb
  • 谢谢@Syscall。最后我已经解决了我的问题,它正在使用$la-$lb
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-29
  • 1970-01-01
  • 1970-01-01
  • 2017-07-07
  • 2020-05-08
  • 2020-07-30
相关资源
最近更新 更多