将数组中某一元素值相同的追加到该数组
程序员文章站
2024-02-02 14:16:46
...
Array( [0] => Array ( [num] => 1 [sendPhone] => 15670015154 [sendDate] => 2015-01-12 01:19:57 [sendStatus] => 1 ) [1] => Array ( [num] => 1 [sendPhone] => 15518788555 [sendDate] => 2015-01-12 02:28:18 [sendStatus] => 0 ) [2] => Array ( [num] => 1 [sendPhone] => 15518788555 [sendDate] => 2015-01-12 02:05:10 [sendStatus] => 1 ) [3] => Array ( [num] => 1 [sendPhone] => 15518788544 [sendDate] => 2015-01-12 02:11:38 [sendStatus] => 1 ) [4] => Array ( [num] => 2 [sendPhone] => 15670015154 [sendDate] => 2015-01-12 02:28:18 [sendStatus] => 0 ))
sendPhone是相同的,想实现为下面的数组,数组中sendStatus=1表示发送成功sendStatus=0表示失败,num=1,表示为1条信息,时间取最大时间,我是想把数组组合成方便遍历的模式。请教怎么写,
Array( [0] => Array ( [sendPhone] => 15670015154 [sendDate] => 2015-01-12 02:28:18 [success] =>1, [failure]=>2, ) [1] => Array ( [num] => 1 [sendPhone] => 15518788555 [sendDate] => 2015-01-12 02:05:10 [success] => 1, [failure]=>1 ) [2] => Array ( [num] => 1 [sendPhone] => 15518788544 [sendDate] => 2015-01-12 02:11:38 [success] => 1, [failure]=> ))
回复讨论(解决方案)
$ar = array ( 0 => array ( 'num' => '1', 'sendPhone' => '15670015154', 'sendDate' => '2015-01-12 01:19:57', 'sendStatus' => '1', ), 1 => array ( 'num' => '1', 'sendPhone' => '15518788555', 'sendDate' => '2015-01-12 02:28:18', 'sendStatus' => '0', ), 2 => array ( 'num' => '1', 'sendPhone' => '15518788555', 'sendDate' => '2015-01-12 02:05:10', 'sendStatus' => '1', ), 3 => array ( 'num' => '1', 'sendPhone' => '15518788544', 'sendDate' => '2015-01-12 02:11:38', 'sendStatus' => '1', ), 4 => array ( 'num' => '2', 'sendPhone' => '15670015154', 'sendDate' => '2015-01-12 02:28:18', 'sendStatus' => '0', ),);$res = array();foreach($ar as $item) { $k = $item['sendPhone']; if(! isset($res[$k])) { $res[$k] = array( 'sendPhone' => $k, 'sendDate' => '', 'success' => 0, 'failure' => 0, ); } $res[$k]['sendDate'] = max($res[$k]['sendDate'], $item['sendDate']); $res[$k]['success'] += $item['sendStatus'] ? $item['num'] : 0; $res[$k]['failure'] += ! $item['sendStatus'] ? $item['num'] : 0;}print_r(array_values($res));
Array( [0] => Array ( [sendPhone] => 15670015154 [sendDate] => 2015-01-12 02:28:18 [success] => 1 [failure] => 2 ) [1] => Array ( [sendPhone] => 15518788555 [sendDate] => 2015-01-12 02:28:18 [success] => 1 [failure] => 1 ) [2] => Array ( [sendPhone] => 15518788544 [sendDate] => 2015-01-12 02:11:38 [success] => 1 [failure] => 0 ))
$ar = array ( 0 => array ( 'num' => '1', 'sendPhone' => '15670015154', 'sendDate' => '2015-01-12 01:19:57', 'sendStatus' => '1', ), 1 => array ( 'num' => '1', 'sendPhone' => '15518788555', 'sendDate' => '2015-01-12 02:28:18', 'sendStatus' => '0', ), 2 => array ( 'num' => '1', 'sendPhone' => '15518788555', 'sendDate' => '2015-01-12 02:05:10', 'sendStatus' => '1', ), 3 => array ( 'num' => '1', 'sendPhone' => '15518788544', 'sendDate' => '2015-01-12 02:11:38', 'sendStatus' => '1', ), 4 => array ( 'num' => '2', 'sendPhone' => '15670015154', 'sendDate' => '2015-01-12 02:28:18', 'sendStatus' => '0', ),);$res = array();foreach($ar as $item) { $k = $item['sendPhone']; if(! isset($res[$k])) { $res[$k] = array( 'sendPhone' => $k, 'sendDate' => '', 'success' => 0, 'failure' => 0, ); } $res[$k]['sendDate'] = max($res[$k]['sendDate'], $item['sendDate']); $res[$k]['success'] += $item['sendStatus'] ? $item['num'] : 0; $res[$k]['failure'] += ! $item['sendStatus'] ? $item['num'] : 0;}print_r(array_values($res));
Array( [0] => Array ( [sendPhone] => 15670015154 [sendDate] => 2015-01-12 02:28:18 [success] => 1 [failure] => 2 ) [1] => Array ( [sendPhone] => 15518788555 [sendDate] => 2015-01-12 02:28:18 [success] => 1 [failure] => 1 ) [2] => Array ( [sendPhone] => 15518788544 [sendDate] => 2015-01-12 02:11:38 [success] => 1 [failure] => 0 ))
成功了。谢谢版主的支持,逻辑原来是这样的!,
上一篇: MySQL数据库优化详解_MySQL
下一篇: MySQL管理与优化(1)_MySQL
推荐阅读
-
将数组中某一元素值相同的追加到该数组
-
有15个数按由大到小顺序存放在一个数组中,输入一个数,要求用折半查找法找出该数是数组中 第几个元素的值。如果该数不在数组中,则输出“无此数”。
-
有15个数按由大到小顺序存放在一个数组中,输入一个数,要求用折半查找法找出该数是数组中第几个元素的值。如果该数不在数组中,则输出“无此数”——C语言
-
有15个数存放在一个数组中,输入一个数,要求查找该数是数组中第几个元素的值。如果该数不在数组中,则输出“无此数”。
-
有15个数按从小到大顺序存放在一个数组中,输入一个数,输出一个数,要求用折半查找法找出该数是数组中第几个元素的值。如果该数不在数组中,则输出“无此数”。
-
js实现以最简单的方式将数组元素添加到对象中的方法
-
php 删除一维数组中某一个值元素的操作方法
-
Java经典编程习题100例:第18例:编写程序,将一个数组中的元素倒排过来。例如原数组为1,2,3,4,5;则倒排后数组中的值
-
输入10个互不相同的整数并保存在数组中,找到该最大元素并删除它,输出删除后的数组
-
PHP实现删除二维数组中相同元素及数组重复值的方法