简体   繁体   中英

json array sort by value in php

I have JSON arrays of objects. I am trying to sort an array using usort. I want to use value field from field_listing_order . It sorted using value. I am missing something but not able to figure it out. please review the code. Thanks!

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
                                                )

                                        )

                                )

                       )

                 )

        )

)

and So on ...

  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);
   }); ?>

If you need to sort all the nodes using the field_listing_order value, you need to compare the values along the full path, from the first object to the value:

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 ;
});

In this case $a and $b are two different nodes which could be compared. That why you should compare the field_listing_order 's value of these nodes. The answer is working with $la- $lb

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM