Heim >Backend-Entwicklung >PHP-Tutorial >PHP 多维数组有关问题,有点难度

PHP 多维数组有关问题,有点难度

WBOY
WBOYOriginal
2016-06-13 11:51:531036Durchsuche

PHP 多维数组问题,有点难度!
这样两个数组:

<br />//注册信息<br />$array1 = array(<br />0=>array(<br />'day'=>'2014-3-7',//注册日期<br />'data'=>array(<br />0=>array('uid'=>'0000001'),<br />1=>array('uid'=>'0000002'),<br />3=>array('uid'=>'0000003'),<br />4=>array('uid'=>'0000004'),<br />)<br />),<br />1=>array(<br />'day'=>'2014-3-8',//注册日期<br />'data'=>array(<br />0=>array('uid'=>'0000005'),<br />1=>array('uid'=>'0000007'),<br />2=>array('uid'=>'0000006'),<br />)<br />),<br />2=>array(<br />'day'=>'2014-3-10',//注册日期<br />'data'=>array(<br />0=>array('uid'=>'0000008'),<br />1=>array('uid'=>'0000010'),<br />2=>array('uid'=>'0000009'),<br />3=>array('uid'=>'0000012'),<br />4=>array('uid'=>'0000013'),<br />)<br />),<br />3=>array(<br />'day'=>'2014-3-11',//注册日期<br />'data'=>array(<br />0=>array('uid'=>'0000015'),<br />1=>array('uid'=>'0000014'),<br />2=>array('uid'=>'0000025'),<br />3=>array('uid'=>'0000017'),<br />4=>array('uid'=>'0000018'),<br />)<br />),<br />);<br /><br />//登录日志<br />$array2 = array(<br />0=>array(<br />'day'=>'2014-3-8',//登录日期<br />'ge_data'=>array(<br />0=>array('uid'=>'0000001'),<br />1=>array('uid'=>'0000002'),<br />3=>array('uid'=>'0000003')<br />)<br />),<br />1=>array(<br />'day'=>'2014-3-9',//登录日期<br />'ge_data'=>array(<br />0=>array('uid'=>'0000002'),<br />1=>array('uid'=>'0000007'),<br />2=>array('uid'=>'0000006'),<br />)<br />),<br />2=>array(<br />'day'=>'2014-3-11',//登录日期<br />'ge_data'=>array(<br />0=>array('uid'=>'0000001'),<br />1=>array('uid'=>'0000007'),<br />2=>array('uid'=>'0000008'),<br />3=>array('uid'=>'0000012'),<br />4=>array('uid'=>'0000013'),<br />5=>array('uid'=>'0000003'),<br />6=>array('uid'=>'0000002')<br />)<br />)<br />);<br />


如何能得到以下数组:


各位帮帮忙!
------解决方案--------------------
可以这样写
//注册信息<br />$array1 = array(<br />0=>array(<br />'day'=>'2014-3-7',//注册日期<br />'data'=>array(<br />0=>array('uid'=>'0000001'),<br />1=>array('uid'=>'0000002'),<br />3=>array('uid'=>'0000003'),<br />4=>array('uid'=>'0000004'),<br />)<br />),<br />1=>array(<br />'day'=>'2014-3-8',//注册日期<br />'data'=>array(<br />0=>array('uid'=>'0000005'),<br />1=>array('uid'=>'0000007'),<br />2=>array('uid'=>'0000006'),<br />)<br />),<br />2=>array(<br />'day'=>'2014-3-10',//注册日期<br />'data'=>array(<br />0=>array('uid'=>'0000008'),<br />1=>array('uid'=>'0000010'),<br />2=>array('uid'=>'0000009'),<br />3=>array('uid'=>'0000012'),<br />4=>array('uid'=>'0000013'),<br />)<br />),<br />3=>array(<br />'day'=>'2014-3-11',//注册日期<br />'data'=>array(<br />0=>array('uid'=>'0000015'),<br />1=>array('uid'=>'0000014'),<br />2=>array('uid'=>'0000025'),<br />3=>array('uid'=>'0000017'),<br />4=>array('uid'=>'0000018'),<br />)<br />),<br />);<br /> <br />//登录日志<br />$array2 = array(<br />0=>array(<br />'day'=>'2014-3-8',//登录日期<br />'ge_data'=>array(<br />0=>array('uid'=>'0000001'),<br />1=>array('uid'=>'0000002'),<br />3=>array('uid'=>'0000003')<br />)<br />),<br />1=>array(<br />'day'=>'2014-3-9',//登录日期<br />'ge_data'=>array(<br />0=>array('uid'=>'0000002'),<br />1=>array('uid'=>'0000007'),<br />2=>array('uid'=>'0000006'),<br />)<br />),<br />2=>array(<br />'day'=>'2014-3-11',//登录日期<br />'ge_data'=>array(<br />0=>array('uid'=>'0000001'),<br />1=>array('uid'=>'0000007'),<br />2=>array('uid'=>'0000008'),<br />3=>array('uid'=>'0000012'),<br />4=>array('uid'=>'0000013'),<br />5=>array('uid'=>'0000003'),<br />6=>array('uid'=>'0000002')<br />)<br />)<br />);<br /><br />$res = array();<br /><br />//读取每天的注册记录<br />foreach($array1 as $r) {<br />  $day = $r['day'];<br />  $res[$day] = array('day' => $day, 'count' => count($r['data']));<br />  $d = date('z', strtotime($day));<br /><br />  //对于当日注册每个用户<br />  foreach($r['data'] as $us) {<br />    foreach($array2 as $t) {<br />      $rd = date('z', strtotime($t['day']));<br />      //检查他在哪天登录了<br />      foreach($t['ge_data'] as $u) {<br />        if($us['uid'] == $u['uid']) $res[$day]['day_'.($rd-$d)][] = $us['uid']; //保存登录的uid以便于检查<br />      }<br />    }<br />  }<br />}<br />print_r($res);

Array<br>(<br>    [2014-3-7] => Array<br>        (<br>            [day] => 2014-3-7<br>            [count] => 4<br>            [day_1] => Array<br>                (<br>                    [0] => 0000001<br>                    [1] => 0000002<div class="clear">
                 
              
              
        
            </div>
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Vorheriger Artikel:php错误处理机制如何解决Nächster Artikel:php中文api(带例证)