0

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 etc
i 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.

1
  • decode json + foreach($array as $value) $rtarray[strtolower($value['category'])]++; Commented Mar 23, 2011 at 13:55

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

4 Comments

i decode the json file and apply the method but not get my result
Apply the method (as ideea),not the function :). After you decode it you should get an array like $array['data'], I'll give you the rest above in a sec
Using $value['category'] as index gives an "undefined index" exception, whats the right approach to solve this? :)
for the love of God Dane411 :)), replace $categCount[$value['category']]++; with (!empty($categCount[$value['category']])?$categCount[$value['category']]++:$categCount[$value['category']]=1);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.