根据属性值选择对象的对象数组

array of objects selecting object based on attribute value

我目前正在使用 Mailchimp API,我有一份已经 运行 或即将 运行 的活动列表,我想获取最近 运行 活动的 link。我将如何比较属性 "send_time" 以找到最近的和它的属性父对象?

活动数组如下所示,

{
    "campaigns": [
        {
            "id": 1,
            "type": "regular",
            "status": "save",
            "send_time": ""
        },
        {
            "id": 2,
            "type": "regular",
            "status": "sent",
            "send_time": "2015-11-11T14:42:58+00:00"
        },
        {
            "id": 3,
            "type": "regular",
            "status": "sent",
            "send_time": "2016-01-01T14:42:58+00:00"
        },
        {
            "id": 4,
            "type": "regular",
            "status": "sent",
            "send_time": "2016-06-12T14:42:58+00:00"
        }
    ]
}

所以在上面的数组中,最终对象有最近的 send_time,我将如何评估这个,然后抓取那个对象?我有一个半解决方案,但似乎很啰嗦。

<?php
    //Build an array of send_times
    $dates = [];
    foreach($result['campaigns'] as $campaign) {
        $dates[$campaign['id']] = $campaign['send_time'];
    }

    //Get the most recent date
    $mostRecent = 0;
    foreach($dates as $k => $v) {
        $curDate = strtotime($v);
        if($curDate > $mostRecent) {
           $mostRecent = $curDate
           $currentId = $k;
        }
    }

    //Get the object
    foreach($results['campaigns'] as $campaign) {
         if($campaign['id'] == $currentId) {
             $c = $campaign;
         }
    }
?>

您可以按 属性 对对象进行排序(我建议 usort,这样您就可以定义自己的排序方式),然后获取该数组中的第一项。

usort($result, function ($campaign1, $campaign2) {
    if ($campaign1['send_time'] == $campaign2['send_time']) {
        return 0;
    }

    return (strtotime($campaign1['send_time']) > strtotime($campaign2['send_time'])) ? -1 : 1;
});

$mostRecentCampaign = $campaign[0];

注意:我没有运行这个,所以如果排序错误,你可能需要调整比较函数的return订单。

<?php
    $dates = [];
    $recent_campaign = null;
    $recent_time = 0;
    foreach($result['campaigns'] as $campaign) {
        $curDate = strtotime($campaign['send_time']);
        if($curDate > $recent_time) {
            $recent_time = $curDate
            $recent_campaign = $campaign;
        }
    }
    //$recent_campaign is the most recent campaign 
?>

你可以试试这个方法。否则你可以使用 usort by send_time (直接解决方案)。

我没有执行过这段代码!

如果你只是想获取最大元素并且想使用更少的代码行,试试这个:

  1. 通过将 "send time" 列映射到纪元时间并获取最大值来获取最大时间
  2. 通过对应于该最大时间的条目过滤您的数组
  3. 利润

示例:

$dataArray = /* your array */
$maxTime =  max(array_map('strtotime',array_column($dataArray["campaigns"], 'send_time')));
$maxEntry = array_filter($dataArray["campaigns"], function ($arr) use ($maxTime) { return strtotime($arr["send_time"])==$maxTime; });
print_r($maxEntry);

将打印:

Array
(
    [3] => Array
        (
            [id] => 4
            [type] => regular
            [status] => sent
            [send_time] => 2016-06-12T14:42:58+00:00
        )

)

注意这样做的好处是不需要排序。缺点是排序然后获取最后一个元素会更快。但是,排序会丢失有时需要的原始数组顺序。

像下面这样使用array_multisort()(单行代码):-

<?php    
$data = '{
    "campaigns": [
        {
            "id": 1,
            "type": "regular",
            "status": "save",
            "send_time": ""
        },
        {
            "id": 2,
            "type": "regular",
            "status": "sent",
            "send_time": "2015-11-11T14:42:58+00:00"
        },
        {
            "id": 3,
            "type": "regular",
            "status": "sent",
            "send_time": "2016-01-01T14:42:58+00:00"
        },
        {
            "id": 4,
            "type": "regular",
            "status": "sent",
            "send_time": "2016-06-12T14:42:58+00:00"
        }
    ]
}';
$array = json_decode($data,true)['campaigns']; // decode json string to array  
array_multisort($array,SORT_DESC, SORT_STRING); // use of array_multisort
echo "<pre/>";print_r($array); // this will returns you indexed array like 0,1,2... you can again convert it to campaigns array like $final_array['campaigns'] = $array;
?>

输出:- https://eval.in/598349

注:-

1.If 你给的数据已经在数组里了,不用json_decode(),直接用array_multisort()就可以了

https://eval.in/598355

更多参考:-

http://sg2.php.net/manual/en/function.array-multisort.php