1

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
                        )
               )
        )
)
6
  • That is an array not an object. Use array notation Commented Feb 2, 2016 at 10:42
  • Looks like you need to iterate through the trade_offers_received object first, then access the time_created attribute inside each iteration. Commented Feb 2, 2016 at 10:42
  • @Defiant I tryed this also but its not working Commented Feb 2, 2016 at 10:43
  • 1
    If you want an object, remove the TRUE param from the json_decode() Commented Feb 2, 2016 at 10:43
  • @RiggsFolly array_filter() expects parameter 1 to be array. Commented Feb 2, 2016 at 10:47

2 Answers 2

2

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 );
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks , I ll try . Basicly what am trying to do is found how many offers are there with time_created > time()-10. I am trying first to filter the array and than count it using count($datafilter['response']['trade_offers_received']) . Currently am getting this errors Undefined variable: time and : Array to string conversion when am trying to echo $datafilter
To pass $time to the closure, you need to use the USE($time) syntax. Refresh the page as I missed that first time round
Thanks . I am still getting this Notice: Array to string conversion in C:\xampp\htdocs\offer\funkcija.php on line 15. Line 15 is echo $datafilter;
Ok change that to a print_r( $datafilter ); as its an array of course. I am really not paying attention today. Sorry
Nice , U are helped me a lot. Its working perfectly now. Thanks
-1

When you use TRUE as the second parameter of json_decode, it becomes an Array. If you want to get an object in return, remove the second parameter.

    $data = json_decode($json); //returns an object
    $arr  = json_decode($json); //returns an array

2 Comments

You might want to check that!
Its not particularly relevant to the question anyway as he wants an array so he can feed it into the array_filter() function. Not always a good idea to jump on comments and try to vampire them into answers!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.