$ 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' => 'u.s., 'Province '=> 'onon '),
- Array ('id' => 9, 'CID' => 2, 'Country' => 'u.s., 'Province '=> 'onon '),
- Array ('id' => 10, 'CID' => 3, 'Country' => 'Japan ', 'Province' => 'nagoya '),
- );
// Reference answer:
- /*
- 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:
- $ 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; // determine the key points of this question.
- }
- // Final data structure
- Echo 'Country: ', count ($ data ),'
';
- Foreach ($ data as $ k => $ v ){
- Echo '', $ k, ':', array_sum ($ v), 'sub ';
- Echo 'Province: ', count ($ v ),'
';
- Foreach ($ v as $ kk => $ vv ){
- Echo '', $ kk, ':', $ vv, 'Times
';
- }
}
- ?>
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:
- Foreach ($ arr as $ k =>$ v ){
- @ $ Data [$ v ['country'] [$ v ['Province '] + = 1; // determine the key points of this question.
- }
-
Master the practical skills of php array. |