title: Efficiently find nested PHP array element based on attribute value
body: <p>Suppose a PHP array, when cast to JSON, has the following format:</p>
  
  <pre><code>[{
      "key": "width",
      "value": "1200",
      "label": "Width (mm)",
      "choice": ""
  },
  {
      "key": "height",
      "value": "900",
      "label": "Height (mm)",
      "choice": ""
  },
  {
      "key": "material",
      "value": "paper",
      "label": "Material",
      "choice": "Paper"
  }]
  </code></pre>
  
  <p>(This is a shortened version of the original, which can have many more elements)</p>
  
  <p>Let's suppose I want to efficiently find what material is used. In other words, I want to search for a nested array that has for <code>key</code> the value <code>material</code>, and I want to return the <code>value</code> which would be <code>paper</code>.</p>
  
  <p>I know this can be done by using a foreach/while loop, but PHP is rich with compiled array functions that I'm not very familiar with. What's the best function to use here?</p>
  
  <hr>
  
  <p><strong>UPDATE: What I've tried so far</strong></p>
  
  <p>Here's two things I've tried so far:</p>
  
  <p>Attempt #1:</p>
  
  <pre><code>$json = '[{"key":"width","value":"1200","label":"Width (mm)","choice":""},{"key":"height","value":"900","label":"Height (mm)","choice":""},{"key":"material","value":"paper","label":"Material","choice":"Paper"}]';
  $array = json_encode($json, true);
  $material = '';
  foreach($array as $nestedArray) {
    if($nestedArray['key'] = 'material') {
      $material = $nestedArray['value'];
    }
  }
  </code></pre>
  
  <p>Attempt #2:</p>
  
  <pre><code>$json = '[{"key":"width","value":"1200","label":"Width (mm)","choice":""},{"key":"height","value":"900","label":"Height (mm)","choice":""},{"key":"material","value":"paper","label":"Material","choice":"Paper"}]';
  $array = json_decode($json, true);
  $filteredArray = array_filter($array, function($array) {
      return ($array['key'] == 'material');
  });
  $arr = array_pop($filteredArray)['value'];
  </code></pre>
  
  <hr>
  
  <p>Both produce the right value, but #1 is messy, and #2 may not be the best use of PHPs array functions.</p>