我有F8等JSON文件,我想存儲和在PHP即,對於簡單的數組的數組計數的類別如何可以計數不同的值在PHP關聯數組
function array_icount_values($array) { $rtarray = array(); foreach($array as $value) $rtarray[strtolower($value)]++; return $rtarray; $ar = array('red', 'green', 'blue', 'red', 'red', 'red', 'blue', 'blue', 'green'); //$ar = array_icount_values($re);
其輸出將是
[red]=>4 [blue]=>3 [green]=>2
我想同樣的結果,此文件
{ "data": [ { "name": "The Lord of the Rings Trilogy (Official Page)", "category": "Movie" }, { "name": "Snatch", "category": "Movie" }, { "name": "The Social Network Movie", "category": "Movie" }, { "name": "Scarface", "category": "public figure" }, { "name": "Johnny Depp", "category": "Actor/director" }, { "name": "Once Upon a Time in the West", "category": "public figure" }, { "name": "Legend of the Guardians: The Owls of Ga'Hoole", "category": "public figure" }, { "name": "Once Upon a Time in America", "category": "public figure" }, { "name": "Butch Cassidy and the Sundance Kid", "category": "public figure" }, { "name": "Fracture", "category": "public figure" }, { "name": "Invictus", "category": "public figure" }, { "name": "Pride and Glory", "category": "public figure" } ] }
注,我只是想要的類別數,即
[Movies]=>5 [Tv show]=>4 etc我使用此代碼
function array_icount_values($array) { $ret_ar=array(); foreach($array['data'] as $key=>$val) { print_r(array_count_values($val)); }} $string = file_get_contents("likes.json"); $json_a=json_decode($string,true); $re=array_icount_values($json_a);但這給了奇怪的結果。
解碼json +'foreach($ array as $ value)$ rtarray [strtolower($ value ['category'])] ++;' – biakaveron 2011-03-23 13:55:22
看我更新的問題 – hunter 2011-03-23 13:59:09