Search code examples
phparraysjsonarray-filter

How to access object array in php


I am trying to filter it using array_filter

$time = (time() -10);

$data = json_decode($json, TRUE);

$datafilter = array_filter($data, function($obj)
        {
        return $obj->response->trade_offers_received->time_created > $time;
        });

echo $datafilter;

I am getting this notice: Trying to get property of non-object. So am doing something wrong. How can i filter this array to get only object where time_created > $time ?

This is my JSON data

Array
(
    [response] => Array
        (
            [trade_offers_received] => Array
                (
                    [0] => Array
                        (
                            [tradeofferid] => 988832058
                            [accountid_other] => 212629269
                            [message] => 
                            [expiration_time] => 1455467839
                            [trade_offer_state] => 2
                            [items_to_give] => Array
                                (
                                    [0] => Array
                                        (
                                            [appid] => 730
                                            [contextid] => 2
                                            [assetid] => 4974401193
                                            [classid] => 319730196
                                            [instanceid] => 188530139
                                            [amount] => 1
                                            [missing] => 
                                        )

                                )

                            [items_to_receive] => Array
                                (
                                    [0] => Array
                                        (
                                            [appid] => 730
                                            [contextid] => 2
                                            [assetid] => 4981322842
                                            [classid] => 311848115
                                            [instanceid] => 188530139
                                            [amount] => 1
                                            [missing] => 
                                        )

                                    [1] => Array
                                        (
                                            [appid] => 730
                                            [contextid] => 2
                                            [assetid] => 3600963191
                                            [classid] => 350875311
                                            [instanceid] => 480085569
                                            [amount] => 1
                                            [missing] => 
                                        )
                                )

                            [is_our_offer] => 
                            [time_created] => 1454258239
                            [time_updated] => 1454258249
                            [from_real_time_trade] => 
                            [escrow_end_date] => 0
                            [confirmation_method] => 0
                        )
                    [1] => Array
                        (
                            [tradeofferid] => 988791916
                            [accountid_other] => 55306956
                            [message] => fv 0.1616 nice look
                            [expiration_time] => 1455466526
                            [trade_offer_state] => 2
                            [items_to_give] => Array
                                (
                                    [0] => Array
                                        (
                                            [appid] => 730
                                            [contextid] => 2
                                            [assetid] => 4974401224
                                            [classid] => 311236182
                                            [instanceid] => 188530139
                                            [amount] => 1
                                            [missing] => 
                                        )
                                )
                            [items_to_receive] => Array
                                (
                                    [0] => Array
                                        (
                                            [appid] => 730
                                            [contextid] => 2
                                            [assetid] => 4985509468
                                            [classid] => 310992880
                                            [instanceid] => 188530139
                                            [amount] => 1
                                            [missing] => 
                                        )
                                )

                            [is_our_offer] => 
                            [time_created] => 1454256926
                            [time_updated] => 1454256936
                            [from_real_time_trade] => 
                            [escrow_end_date] => 0
                            [confirmation_method] => 0
                        )
               )
        )
)

Solution

  • As I am not sure what you will get if you do the json_decode() without the TRUE parameter that converts objects into arrays, we will stick with the array notation.

    Also if you are only interested in data that exists at the $data['response']['trade_offers_received'] level of this data structure, start your array_filter at that level.

    But mainly as they are arrays, use the array notation and not the object notation and things will probably work

    More Importantly: You will also need to amend the closure code to pass the $time variable into the closure.

    $time = (time() -10);
    
    $data = json_decode($json, TRUE);
    
    $datafilter = array_filter($data['response']['trade_offers_received'],
                                  function($arr) use($time)
                                  {
                                      return $arr['time_created'] > $time;
                                  }
                               );
    
    print_r( $datafilter );