Heim > Artikel > Backend-Entwicklung > 如何获取对应数组的结果。形成新的对应数组
$t1 = Array ( 0 => '南昌', 1 => '南昌', 2 => '赣州', 3 => '九江', 4 => '赣州', 5 => '九江');
//第一步$t1 = Array ( 0 => '南昌', 1 => '南昌', 2 => '赣州', 3 => '九江', 4 => '赣州', 5 => '九江'); $t2 = Array ( 0 => '优秀', 1 => '良好', 2 => '优秀', 3 => '良好', 4 => '优秀', 5 => '差等' );$t = array_map(null, $t1, $t2);print_r($t);/* 得到Array( [0] => Array ( [0] => 南昌 [1] => 优秀 ) [1] => Array ( [0] => 南昌 [1] => 良好 ) [2] => Array ( [0] => 赣州 [1] => 优秀 ) [3] => Array ( [0] => 九江 [1] => 良好 ) [4] => Array ( [0] => 赣州 [1] => 优秀 ) [5] => Array ( [0] => 九江 [1] => 差等 ))*///第二步$r = array();foreach($t as $v) { $r[$v[0]][] = $v[1];}print_r($r);/*得到Array( [南昌] => Array ( [0] => 优秀 [1] => 良好 ) [赣州] => Array ( [0] => 优秀 [1] => 优秀 ) [九江] => Array ( [0] => 良好 [1] => 差等 ))*///第三步$r = array_map('array_count_values', $r);print_r($r);/*得到Array( [南昌] => Array ( [优秀] => 1 [良好] => 1 ) [赣州] => Array ( [优秀] => 2 ) [九江] => Array ( [良好] => 1 [差等] => 1 ))
接下来就不用说了
请高人指点咯。还是不太清楚。
//第一步$t1 = Array ( 0 => '南昌', 1 => '南昌', 2 => '赣州', 3 => '九江', 4 => '赣州', 5 => '九江'); $t2 = Array ( 0 => '优秀', 1 => '良好', 2 => '优秀', 3 => '良好', 4 => '优秀', 5 => '差等' );$t = array_map(null, $t1, $t2);print_r($t);/* 得到Array( [0] => Array ( [0] => 南昌 [1] => 优秀 ) [1] => Array ( [0] => 南昌 [1] => 良好 ) [2] => Array ( [0] => 赣州 [1] => 优秀 ) [3] => Array ( [0] => 九江 [1] => 良好 ) [4] => Array ( [0] => 赣州 [1] => 优秀 ) [5] => Array ( [0] => 九江 [1] => 差等 ))*///第二步$r = array();foreach($t as $v) { $r[$v[0]][] = $v[1];}print_r($r);/*得到Array( [南昌] => Array ( [0] => 优秀 [1] => 良好 ) [赣州] => Array ( [0] => 优秀 [1] => 优秀 ) [九江] => Array ( [0] => 良好 [1] => 差等 ))*///第三步$r = array_map('array_count_values', $r);print_r($r);/*得到Array( [南昌] => Array ( [优秀] => 1 [良好] => 1 ) [赣州] => Array ( [优秀] => 2 ) [九江] => Array ( [良好] => 1 [差等] => 1 ))
请高人说完。。谢谢了。
其实说的够明显了,稍微改一下代码
$t1 = Array ( 0 => '南昌', 1 => '南昌', 2 => '赣州', 3 => '九江', 4 => '赣州', 5 => '九江'); $t2 = Array ( 0 => '优秀', 1 => '良好', 2 => '优秀', 3 => '良好', 4 => '优秀', 5 => '差等' ); $t = array_map(null, $t1, $t2); $r = array(); foreach($t as $v) { $r[$v[1]][] = $v[0]; } $r = array_map('array_count_values', $r); dump($r);//--------------------------------array(3) { ["优秀"] => array(2) { ["南昌"] => int(1) ["赣州"] => int(2) } ["良好"] => array(2) { ["南昌"] => int(1) ["九江"] => int(1) } ["差等"] => array(1) { ["九江"] => int(1) }}