Tips on using PHP arrays

Source: Internet
Author: User
Tips on using PHP arrays

  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' => 'u.s., 'Province '=> 'onon '),
  10. Array ('id' => 9, 'CID' => 2, 'Country' => 'u.s., 'Province '=> 'onon '),
  11. Array ('id' => 10, 'CID' => 3, 'Country' => 'Japan ', 'Province' => 'nagoya '),
  12. );

  13. // Reference answer:

  14. /*
  15. Operations on data are basically re-constructing the data in the array. learning how to construct the array structure also master the data usage method. The homepage of this question needs to construct the final array structure to be output. this array structure can be final or indirect. The following is an array construction method:
  16. $ Ary = array (
  17. 'China' => array ('hunan '=> 2, 'Shandong' => 2, 'Beijing' => 1 ),
  18. 'Japan '=> array ('nagoya' => 2, 'Tokyo '=> 1 ),
  19. 'America '=> array (),
  20. );
  21. */

  22. $ Data = array ();

  23. Foreach ($ arr as $ k =>$ v ){
  24. @ $ Data [$ v ['country'] [$ v ['Province '] + = 1; // determine the key points of this question.
  25. }
  26. // Final data structure
  27. Echo 'Country: ', count ($ data ),'
    ';
  28. Foreach ($ data as $ k => $ v ){
  29. Echo '', $ k, ':', array_sum ($ v), 'sub ';
  30. Echo 'Province: ', count ($ v ),'
    ';
  31. Foreach ($ v as $ kk => $ vv ){
  32. Echo '', $ kk, ':', $ vv, 'Times
    ';
  33. }

  34. }

  35. ?>

Output: country: 3 China: 5 provinces: 3 Hunan: 2 Beijing: 1 Shandong: 2 Japan: 3 provinces: 2 Nagoya: 2 times in Tokyo: 1 time in the United States: 2 times in the province: 1 time in Ontario: 2 times out of interest. I have a good understanding of the array application skills in the code above:

  1. Foreach ($ arr as $ k =>$ v ){
  2. @ $ Data [$ v ['country'] [$ v ['Province '] + = 1; // determine the key points of this question.
  3. }

Master the practical skills of php array.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.