json 数组按 php 中的值排序

json array sort by value in php

我有 JSON 个对象数组。我正在尝试使用 usort 对数组进行排序。我想使用 field_listing_order 中的 value 字段。它使用值排序。我遗漏了一些东西,但无法弄清楚。请查看代码。谢谢!

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 值对所有节点进行排序,则需要比较从第一个对象到值的完整路径上的值:

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