如何在php

时间:2016-02-02 10:39:17

标签: php arrays json array-filter

我正在尝试使用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;

我收到此通知:试图获取非对象的属性。所以我做错了什么。如何过滤此数组以仅获取time_created>的对象$ time?

这是我的JSON数据

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

2 个答案:

答案 0 :(得分:2)

由于我不确定如果你使用id没有将对象转换为数组的json_decode()参数会得到什么,我们将坚持使用数组符号。

此外,如果您只对此数据结构TRUE级别的数据感兴趣,请在该级别开始$data['response']['trade_offers_received']

但主要是因为它们是数组,使用数组符号而不是对象符号,事情可能会起作用

更重要的是: 您还需要修改闭包代码以将array_filter变量传递给闭包。

$time

答案 1 :(得分:-1)

当您使用TRUE作为json_decode的第二个参数时,它将成为一个数组。如果您想获得一个对象,请删除第二个参数。

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