0

I read a few of the threads but none of them seem to match what I need. What I'm trying to do it something like a voting system but does not include mysql at the moment.

I made three radio buttons of music types pop, rock, metallic and a submit so whichever a person choose either one of them and submit the data is wrote into a data.json and stored there.

Then I have a page to load all the data in data.json file but I want to count how many people selected for example metallic and how many people selected rock.

At the moment I'm still testing and I actually thought of an idea but somehow searched up and not getting what I want.....can someone give me a hand?

This is my data.json file

{"type":"metallic"}
{"type":"pop"}
{"type":"metallic"}

and this is what my result code is at the moment

<?php
$file = fopen('data.json', "r");       

$array_record = array();

while(!feof($file))                       
{
    $line = fgets($file);                 
    $data = json_decode($line, TRUE);     

    if (is_array($data))                  
    {                
        foreach ($data as $key => $value) 
        {
           // echo $key.'='.$value.' ';      // show key=value pairs
          $array_record += array($value,);
          var_dump($data);
          print_r($data);
          echo $value;

        }
        echo '<br>'.PHP_EOL;             
    }
}


fclose($file);                          
?>

I know the codes are messy because I tried setting up an empty array first then get the $value which is something like metallic into an array so I can use the array_count_values but the way I'm doing it seems totally wrong or that'd never work. I tried few other ways to get only metallic/pop so I can try counting the same thing.

AH! I just found this topic How to filter JSON array into PHP array

I'm going to read this thread tomorrow and see if it helps. But it wouldn't hurt if you can give me a hand too.

0

1 Answer 1

1

Keep in mind the JSON you have posted is a not a valid JSON.

array_reduce:

<?php
$json = '[{"type":"metallic"},{"type":"pop"},{"type":"metallic"}]';
$result = array_reduce(json_decode($json,TRUE),
    function($v, $item)
    {
        if (!array_key_exists($item['type'], $v))
            $v[$item['type']] = 1;
        else
            $v[$item['type']]++;
        return $v;
    }, array()
);
print_r($result);

Live DEMO.

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

12 Comments

Totally new with json but after you told me that it's invalid I searched around and I know what you meant but weird I am using json_encode to store into the data.json file and what I pasted is what's stored in the data.json and the decode does read the data though.
thanks for the info, not exactly understanding it fully esp. the $v[$item['type']] part but I'll do some more researches~!
@user2128405 $v is the callback result, so what is passed back to $result is $v and $v[$item['type']] means we are making the key of each $v the value or $item['type'], in your case metallic and php and the value a number that increased every time the same type is found. Its also detailed here.
ah, thanks I believe I know what you mean now and reading it slowly and carefully.
this is how I coded my file to be encoded into json. json_encode($_POST).PHP_EOL which is why it's different from what you showed me but if I open up my json and use it with the array_reduce it wouldn't work though
|

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.