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.
foreach($array as $value) $rtarray[strtolower($value['category'])]++;