ホームページ  >  記事  >  バックエンド開発  >  配列、再グループ化に関する質問

配列、再グループ化に関する質問

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

元の配列:

$arr=array(    [0] => Array        (            [caseid] => 31            [name] => 张三            [shijian] => Array                (                    [0] => 8:00                    [1] => 18:00                )        )    [1] => Array        (            [caseid] => 32            [name] => 李四            [shijian] => Array                (                    [0] => 8:00                    [1] => 12:00                    [2] => 18:00                )        )    [2] => Array        (            [caseid] => 33            [name] => 王五            [shijian] => Array                (                    [0] => 8:00                    [1] => 12:00                    [2] => 18:00                )        ))


この配列を再結合し、時間に従って結合したいと思います。結合が完了すると、次のようになります。それを理解するのに長い時間がかかりました。助けて、ありがとう
Array(    [0] => Array        (            [time] => 8:00            [detail] => Array                (                    [0] => Array                        (                            [caseid] => 31                            [casename] => 张三                                                    )					[1] => Array						(							[caseid] => 32							[casename] =>李四													)					[2] => Array						(							[caseid] => 33							[casename] => 王五													)                )        )    [1] => Array        (            [time] => 12:00            [detail] => Array                (                    [0] => Array                        (                            [caseid] => 32                            [casename] => 李四                         )					[1] => Array                        (                            [caseid] => 33                            [casename] => 王五                         )                )        )    [2] => Array        (            [time] => 18:00            [detail] => Array                (                    [0] => Array                        (                            [caseid] => 31                            [casename] => 张三                                                    )					[1] => Array						(							[caseid] => 32							[casename] => 李四													)					[2] => Array						(							[caseid] => 33							[casename] => 王五													)                )        ))



ディスカッションに返信 (解決策)

$arr = array (  0 =>   array (    'caseid' => '31',    'name' => '张三',    'shijian' =>     array (      0 => '8:00',      1 => '18:00',    ),  ),  1 =>   array (    'caseid' => '32',    'name' => '李四',    'shijian' =>     array (      0 => '8:00',      1 => '12:00',      2 => '18:00',    ),  ),  2 =>   array (    'caseid' => '33',    'name' => '王五',    'shijian' =>     array (      0 => '8:00',      1 => '12:00',      2 => '18:00',    ),  ),);foreach($arr as $item) {  foreach($item['shijian'] as $t) {    if(! isset($res[$t])) $res[$t] = array('time' => $t, 'detail' => array());    $res[$t]['detail'][] = array('caseid' => $item['caseid'], 'casename' => $item['name']);  }}array_multisort(array_map('strtotime', array_keys($res)), $res);print_r(array_values($res));
Array(    [0] => Array        (            [time] => 8:00            [detail] => Array                (                    [0] => Array                        (                            [caseid] => 31                            [casename] => 张三                        )                    [1] => Array                        (                            [caseid] => 32                            [casename] => 李四                        )                    [2] => Array                        (                            [caseid] => 33                            [casename] => 王五                        )                )        )    [1] => Array        (            [time] => 12:00            [detail] => Array                (                    [0] => Array                        (                            [caseid] => 32                            [casename] => 李四                        )                    [1] => Array                        (                            [caseid] => 33                            [casename] => 王五                        )                )        )    [2] => Array        (            [time] => 18:00            [detail] => Array                (                    [0] => Array                        (                            [caseid] => 31                            [casename] => 张三                        )                    [1] => Array                        (                            [caseid] => 32                            [casename] => 李四                        )                    [2] => Array                        (                            [caseid] => 33                            [casename] => 王五                        )                )        ))

echo '<meta http-equiv="content-type" content="text/html; charset=utf-8">';$arr = array(    array('caseid'=>31, 'name'=>'张三', 'shijian'=> array('8:00','18:00')),    array('caseid'=>32, 'name'=>'李四', 'shijian'=> array('8:00','12:00','18:00')),    array('caseid'=>33, 'name'=>'王五', 'shijian'=> array('8:00','12:00','18:00')));$result = array();foreach($arr as $row){    foreach($row['shijian'] as $shijian){        $detail = array(            'caseid' => $row['caseid'],            'casename' => $row['name']        );        if(!isset($result[$shijian])){            $result[$shijian]['time'] = $shijian;            $result[$shijian]['detail'] = array();        }        array_push($result[$shijian]['detail'], $detail);    }}ksort($result, SORT_NUMERIC);$result = array_values($result);echo '<pre class="brush:php;toolbar:false">';print_r($result);echo '
';

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。