-
-
$arr = array( - array('id'=>1,'cid' => 1, 'country' => 'China',' province'=>'Hunan'),
- array('id'=>2,'cid' => 1, 'country' => 'China','province'=>'Hunan'),
- array('id'=>3,'cid' => 3, 'country' => 'Japan','province'=>'Nagoya'),
- array('id'=>4 ,'cid' => 3, 'country' => 'Japan','province'=>'Tokyo'),
- array('id'=>5,'cid' => 1, ' country' => 'China','province'=>'Beijing'),
- array('id'=>6,'cid' => 1, 'country' => 'China',' province'=>'Shandong'),
- array('id'=>7,'cid' => 1, 'country' => 'China','province'=>'Shandong'),
- array('id'=>8,'cid' => 2, 'country' => 'United States','province'=>'Ontario'),
- array('id'=>9 ,'cid' => 2, 'country' => 'United States','province'=>'Ontario'),
- array('id'=>10,'cid' => 3, ' country' => 'Japan','province'=>'Nagoya'),
- );
//Reference answer:
- /*
- Basically all operations on data are correct The data in the array is restructured. If you learn how to construct the structure of the array, you will also master how to use the data. The home page of this question needs to construct the array structure to be output at the end. This array structure can be final or indirect. The following is a way of constructing an array:
- $ary =array(
- 'China'=>array('Hunan'=>2,'Shandong'=>2,'Beijing'=>1),
- 'Japan'=>array('Nagoya'=>2,'Tokyo'=>1),
- 'United States'=>array(),
- );
- */
- < p>$data = array();
- foreach($arr as $k=>$v){
- @$data[$v['country']][$v['province']] +=1; //Determine the key points of this question and experience it more.
- }
- //The final structure of the data
- echo 'Country:',count($data),'
';
- foreach($data as $k=>$v){
- echo ' ',$k,':',array_sum($v),'times';
- echo ' Province:',count( $v),'a
';
- foreach($v as $kk=>$vv){
- echo ' ',$kk,' :',$vv,'times
';
- }
}
- ?>
-
Copy the code
Output results:
Countries: 3
China: 5 times Provinces: 3
Hunan: 2 times
Beijing: 1 time
Shandong: 2 times
Japan: 3 times Provinces: 2
Nagoya: 2 times
Tokyo: 1 time
United States: 2 times Province: 1
Ontario: 2 times
Friends who are interested should have a good understanding of the application skills of arrays in the above code and understand this sentence:
-
- foreach($arr as $k=>$v){
- @$data[$v['country']][$v['province']] +=1; //Definitely Please understand the key points of the question.
- }
-
Copy code
Master the practical skills of php arrays.
|