$arr = Array (
- Array (' ID ' =>1, ' cid ' = 1, ' country ' = ' China ', ' province ' = ' Hu '),
- Array (' ID ' =>2, ' cid ' = 1, ' country ' = ' China ', ' province ' = ' Hu '),
- Array (' ID ' =>3, ' cid ' = 3, ' country ' = ' Japan ', ' Province ' and ' Nagoya '),
- Array (' ID ' =>4, ' cid ' = 3, ' country ' = ' Japan ', ' Province ' and ' 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 ' = ' us ', ' province ' = ' Ontario '),
- Array (' ID ' =>9, ' cid ' = 2, ' country ' = ' us ', ' province ' = ' Ontario '),
- Array (' ID ' =>10, ' cid ' = 3, ' country ' = ' Japan ', ' Province ' and ' Nagoya '),
- );
Reference Answer:
- /*
- 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:
- $ary =array (
- ' China ' =>array (' Hunan ' =>2, ' Shandong ' =>2, ' Beijing ' =>1),
- ' Japan ' =>array (' Nagoya ' =>2, ' Tokyo ' =>1),
- ' America ' =>array (),
- );
- */
$data = Array ();
- foreach ($arr as $k = = $v) {
- @ $data [$v [' Country ']][$v [' Province ']] +=1; The focus of the topic, a lot of experience.
- }
- The final structure of the data
- Echo ' Country: ', Count ($data), ' a
';
- 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 CodeOutput: 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:
- foreach ($arr as $k = = $v) {
- @ $data [$v [' Country ']][$v [' Province ']] +=1; The focus of the topic, a lot of experience.
- }
Copy CodeLearn practical tips for PHP arrays. |