Reputation: 1101
i have F8 like json file and i want to store and count the categories in an array i.e for simple array in 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);
its output will be
[red]=>4 [blue]=>3 [green]=>2
i want same result for this file
{ "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" } ] }
Note that i just want the categories count i.e
[Movies]=>5 [Tv show]=>4 etci used this code
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);But this give strange result.
Upvotes: 0
Views: 2745
Reputation: 868
The text you have there is a JSON. Just decode it an use the same method to count categories as in the function you previously provided. Check this out: http://www.php.net/manual/en/function.json-decode.php
Hope I could help, all the best...
LE:
$arr=json_decode($str);
foreach($arr['data'] as $value){
$categCount[$value['category']]++;
}
var_dump($categCount);// should give you the categories count
Upvotes: 1