ホームページ  >  記事  >  バックエンド開発  >  同じ値を持つ配列内の要素を配列に追加します

同じ値を持つ配列内の要素を配列に追加します

WBOY
WBOYオリジナル
2016-06-23 13:41:451127ブラウズ

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



成功の書き方を教えてください。モデレーターのサポートに感謝します。ロジックは次のようになります!、
声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。