title: Fetch JSON data by specific value
body: <p>I'm struggling in making this work and I hope someone can give me a push in the right direction. I'm creating a dashboard which fetches data from multiple sources: Google Analytics, Google Trends and so on. With this being a proof of concept, I'm trying to emulate the different sources by using a JSON file with the following structure:</p>
  
  <pre><code>{
    "data":
    [
      {
        "source":"google analytics",
        "data":
        [
          {
            "month":"jan",
            "visitors":"593"
          },
          {
            ...
          }
        ]
      },
      {
        "source":"google trends",
        "keywords":
        [
          {
            "value":"keyword 1",
            "data":
            [
              {
                "month":"dec",
                "popularity":"10"
              },
              {
                ...
              }
            ]
          },
          {
            "value":"keyword 2",
            "data": ...
          }
        ]
      }
    ]
  }
  </code></pre>
  
  <p>Next I have a dropdown list with keywords from a MySQL database query. When a user selects a keyword from that list, I want the data from the JSON file that matches that specific keyword to show. </p>
  
  <p>For example, when a user selects "keyword 1" from the dropdown list, the values "Dec" and "100" need to be fetched, which will then be used by Chart.js</p>
  
  <p>Right now I have the following piece of code:</p>
  
  <pre><code>$dataFile = json_decode(file_get_contents($src));
  $data = $dataFile-&gt;data[1]-&gt;keywords[0]; // Google Trends
  
  foreach ($data-&gt;results as $result) {
    echo "&lt;li&gt;&lt;p&gt;" . $result-&gt;month . "&lt;/p&gt;&lt;/li&gt;";
    echo "&lt;li&gt;&lt;p&gt;" . $result-&gt;popularity . "&lt;/p&gt;&lt;/li&gt;";
  }
  </code></pre>
  
  <p>This does show the correct data,  being the months and popularity scores, but obviously only for the keyword defined by using the array value. How can I make this show data for the selected keyword (which is posted via GET)?</p>
  
  <p>What I'm trying to do is:</p>
  
  <pre><code>$dataFile = json_decode(file_get_contents($src));
  $data = $dataFile-&gt;data["google trends"]-&gt;keywords[$selectedKeyword];
  
  foreach ($data-&gt;results as $result) {
    echo "&lt;li&gt;&lt;p&gt;" . $result-&gt;month . "&lt;/p&gt;&lt;/li&gt;";
    echo "&lt;li&gt;&lt;p&gt;" . $result-&gt;popularity . "&lt;/p&gt;&lt;/li&gt;";
  }
  </code></pre>
  
  <p>I'm aware of the second parameter of json_decode which makes it an associative array, but I'm not sure how I can use it to accomplish what I'm trying to do..</p>
  
  <p>This is currently the result of $data:</p>
  
  <pre><code>object(stdClass)#10 (2) {
    ["value"]=&gt;
    string(10) "keyword 1"
    ["data"]=&gt;
    array(7) {
      [0]=&gt;
      object(stdClass)#11 (2) {
        ["month"]=&gt;
        string(3) "dec"
        ["popularity"]=&gt;
        string(2) "10"
      }
      [1]=&gt;
      object(stdClass)#12 (2) {
        ["month"]=&gt;
        string(3) "jan"
        ["popularity"]=&gt;
        string(1) "5"
      }
      [2]=&gt;
      object(stdClass)#13 (2) {
        ["month"]=&gt;
        string(3) "feb"
        ["popularity"]=&gt;
        string(1) "8"
      }
      [3]=&gt;
      object(stdClass)#14 (2) {
        ["month"]=&gt;
        string(3) "mar"
        ["popularity"]=&gt;
        string(1) "5"
      }
      [4]=&gt;
      object(stdClass)#15 (2) {
        ["month"]=&gt;
        string(3) "apr"
        ["popularity"]=&gt;
        string(2) "10"
      }
      [5]=&gt;
      object(stdClass)#16 (2) {
        ["month"]=&gt;
        string(3) "mei"
        ["popularity"]=&gt;
        string(2) "20"
      }
      [6]=&gt;
      object(stdClass)#17 (2) {
        ["month"]=&gt;
        string(3) "jun"
        ["popularity"]=&gt;
        string(2) "10"
      }
    }
  }
  </code></pre>