Tips on using PHP arrays

WBOY
Release: 2016-07-25 08:57:41
Original
1058 people have browsed it
  1. $arr = array(

  2. array('id'=>1,'cid' => 1, 'country' => 'China',' province'=>'Hunan'),
  3. array('id'=>2,'cid' => 1, 'country' => 'China','province'=>'Hunan'),
  4. array('id'=>3,'cid' => 3, 'country' => 'Japan','province'=>'Nagoya'),
  5. array('id'=>4 ,'cid' => 3, 'country' => 'Japan','province'=>'Tokyo'),
  6. array('id'=>5,'cid' => 1, ' country' => 'China','province'=>'Beijing'),
  7. array('id'=>6,'cid' => 1, 'country' => 'China',' province'=>'Shandong'),
  8. array('id'=>7,'cid' => 1, 'country' => 'China','province'=>'Shandong'),
  9. array('id'=>8,'cid' => 2, 'country' => 'United States','province'=>'Ontario'),
  10. array('id'=>9 ,'cid' => 2, 'country' => 'United States','province'=>'Ontario'),
  11. array('id'=>10,'cid' => 3, ' country' => 'Japan','province'=>'Nagoya'),
  12. );

  13. //Reference answer:

  14. /*
  15. 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:
  16. $ary =array(
  17. 'China'=>array('Hunan'=>2,'Shandong'=>2,'Beijing'=>1),
  18. 'Japan'=>array('Nagoya'=>2,'Tokyo'=>1),
  19. 'United States'=>array(),
  20. );
  21. */

  22. < p>$data = array();
  23. foreach($arr as $k=>$v){
  24. @$data[$v['country']][$v['province']] +=1; //Determine the key points of this question and experience it more.
  25. }
  26. //The final structure of the data
  27. echo 'Country:',count($data),'
    ';
  28. foreach($data as $k=>$v){
  29. echo '     ',$k,':',array_sum($v),'times';
  30. echo '        Province:',count( $v),'a
    ';
  31. foreach($v as $kk=>$vv){
  32. echo '       ',$kk,' :',$vv,'times
    ';
  33. }

  34. }

  35. ?>

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:

  1. foreach($arr as $k=>$v){
  2. @$data[$v['country']][$v['province']] +=1; //Definitely Please understand the key points of the question.
  3. }
Copy code

Master the practical skills of php arrays.



Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!