首頁 > 後端開發 > php教程 > 将数组中某一元素值相同的追加到该数组

将数组中某一元素值相同的追加到该数组

WBOY
發布: 2016-06-23 13:41:45
原創
1160 人瀏覽過

 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        ))
登入後複製
登入後複製



成功了。谢谢版主的支持,逻辑原来是这样的!,
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板