Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to use php to count the same array?
    text
    copied!<p>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.</p> <p>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.</p> <p>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.</p> <p>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?</p> <p>This is my data.json file</p> <pre><code>{"type":"metallic"} {"type":"pop"} {"type":"metallic"} </code></pre> <p>and this is what my result code is at the moment</p> <pre><code>&lt;?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 =&gt; $value) { // echo $key.'='.$value.' '; // show key=value pairs $array_record += array($value,); var_dump($data); print_r($data); echo $value; } echo '&lt;br&gt;'.PHP_EOL; } } fclose($file); ?&gt; </code></pre> <p>I know the codes are messy because I tried setting up an empty array first then get the <code>$value</code> 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.</p> <p>AH! I just found this topic <a href="https://stackoverflow.com/questions/15489307/how-to-filter-json-array-into-php-array">How to filter JSON array into PHP array</a></p> <p>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. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload