I am working with the Mailchimp API at the moment, I have a list of campaigns that have been run, or are due to be run, and I wanting to get the link for the most recently run campaign. How would I go about comparing the attribute "send_time" to find the most recent and it's attributed parent object?
我目前正在使用Mailchimp API,我有一个已经运行或将要运行的活动列表,我想获得最近运行的活动的链接。如何比较属性“send_time”以查找最近的和属性为父对象?
The campaigns array looks like this,
竞选阵营看起来是这样的,
{
"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"
}
]
}
So in that above array, the final object has the most recent send_time, how would I assess this, and then grab that object? I have a semi solution, but it seems long winded.
在上面的数组中,最终的对象有最近的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;
}
}
?>
4 个解决方案
#1
2
Use array_multisort()
like below (single line code):-
使用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;
?>
Output:- https://eval.in/598349
输出:https://eval.in/598349
Note:-
注意:-
1.If your given data is in array already then no need to use json_decode()
, directly use array_multisort()
on it
1。如果给定的数据已经在数组中,那么不需要使用json_decode(),直接使用array_multisort()
https://eval.in/598355
For more reference:-
更多参考:-
http://sg2.php.net/manual/en/function.array-multisort.php
http://sg2.php.net/manual/en/function.array-multisort.php
#2
2
<?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
?>
You can try this approach. Else you can use usort
by send_time
(direct solution).
您可以尝试这种方法。还可以使用usort by send_time(直接解决方案)。
I have not executed this code!
我没有执行这个代码!
#3
0
You can sort your objects by that property (I'd suggest usort, so that you can define your own sorting) and then get the first item in that array.
您可以通过该属性对对象进行排序(我建议使用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];
Note: I haven't run this, so you might have to tweak the return
on the compare function if it's sorting in the wrong order.
注意:我还没有运行这个函数,所以如果比较函数的排序顺序错误,您可能需要调整比较函数的返回值。
#4
0
If you just want to grab the max element and want to use fewer lines of code, try this:
如果您只想获取max元素并希望使用更少的代码行,请尝试以下操作:
- Grab the max time by mapping the "send time" column to an epoch time and getting the max value
- 通过将“发送时间”列映射到一个纪元时间并获取最大值,获取最大时间。
- Filter your array by the entries that correspond to that max time
- 通过与最大时间对应的条目过滤您的数组。
- Profit
- 利润
Example:
例子:
$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);
Would print:
将打印:
Array
(
[3] => Array
(
[id] => 4
[type] => regular
[status] => sent
[send_time] => 2016-06-12T14:42:58+00:00
)
)
Note The advantage of this is that it doesn't need sorting. The disadvantage is that sorting and then getting the last element would be faster. However with sorting you lose the original array order which is sometimes needed.
注意它的优点是不需要排序。缺点是排序然后获取最后一个元素会更快。但是,由于排序,您会丢失原来的数组顺序,这是有时需要的。
#1
2
Use array_multisort()
like below (single line code):-
使用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;
?>
Output:- https://eval.in/598349
输出:https://eval.in/598349
Note:-
注意:-
1.If your given data is in array already then no need to use json_decode()
, directly use array_multisort()
on it
1。如果给定的数据已经在数组中,那么不需要使用json_decode(),直接使用array_multisort()
https://eval.in/598355
For more reference:-
更多参考:-
http://sg2.php.net/manual/en/function.array-multisort.php
http://sg2.php.net/manual/en/function.array-multisort.php
#2
2
<?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
?>
You can try this approach. Else you can use usort
by send_time
(direct solution).
您可以尝试这种方法。还可以使用usort by send_time(直接解决方案)。
I have not executed this code!
我没有执行这个代码!
#3
0
You can sort your objects by that property (I'd suggest usort, so that you can define your own sorting) and then get the first item in that array.
您可以通过该属性对对象进行排序(我建议使用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];
Note: I haven't run this, so you might have to tweak the return
on the compare function if it's sorting in the wrong order.
注意:我还没有运行这个函数,所以如果比较函数的排序顺序错误,您可能需要调整比较函数的返回值。
#4
0
If you just want to grab the max element and want to use fewer lines of code, try this:
如果您只想获取max元素并希望使用更少的代码行,请尝试以下操作:
- Grab the max time by mapping the "send time" column to an epoch time and getting the max value
- 通过将“发送时间”列映射到一个纪元时间并获取最大值,获取最大时间。
- Filter your array by the entries that correspond to that max time
- 通过与最大时间对应的条目过滤您的数组。
- Profit
- 利润
Example:
例子:
$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);
Would print:
将打印:
Array
(
[3] => Array
(
[id] => 4
[type] => regular
[status] => sent
[send_time] => 2016-06-12T14:42:58+00:00
)
)
Note The advantage of this is that it doesn't need sorting. The disadvantage is that sorting and then getting the last element would be faster. However with sorting you lose the original array order which is sometimes needed.
注意它的优点是不需要排序。缺点是排序然后获取最后一个元素会更快。但是,由于排序,您会丢失原来的数组顺序,这是有时需要的。