Tips for using PHP arrays

Source: Internet
Author: User
  1. $arr = Array (

  2. Array (' ID ' =>1, ' cid ' = 1, ' country ' = ' China ', ' province ' = ' Hu '),
  3. Array (' ID ' =>2, ' cid ' = 1, ' country ' = ' China ', ' province ' = ' Hu '),
  4. Array (' ID ' =>3, ' cid ' = 3, ' country ' = ' Japan ', ' Province ' and ' Nagoya '),
  5. Array (' ID ' =>4, ' cid ' = 3, ' country ' = ' Japan ', ' Province ' and ' 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 ' = ' us ', ' province ' = ' Ontario '),
  10. Array (' ID ' =>9, ' cid ' = 2, ' country ' = ' us ', ' province ' = ' Ontario '),
  11. Array (' ID ' =>10, ' cid ' = 3, ' country ' = ' Japan ', ' Province ' and ' Nagoya '),
  12. );

  13. Reference Answer:

  14. /*
  15. The operation of the data is basically to reconstruct the data in the array, and to learn how to construct the structure of the arrays will also grasp the method of using the data. The first page of this topic needs to construct the last array structure to be output, the array structure can be final, or it can be indirect. Here's how an array is constructed:
  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; The focus of the topic, a lot of experience.
  25. }
  26. The final structure of the data
  27. Echo ' Country: ', Count ($data), ' a
    ';
  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 Code

Output: Country: 3 China: 5 Provinces: 3 Hunan: 2 times Beijing: 1 times Shandong: 2 times Japan: 3 Provinces: 2 Nagoya: 2 times Tokyo: 1 times us: 2 Provinces: one Ontario: 2 interested friends, good taste of the above code in the array of application skills, good understanding of this sentence:

    1. foreach ($arr as $k = = $v) {
    2. @ $data [$v [' Country ']][$v [' Province ']] +=1; The focus of the topic, a lot of experience.
    3. }
Copy Code

Learn practical tips for PHP arrays.

  • 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.