title: PHP - search for a key value and then find the value of another key in the same object subarray
body: <p>I've searched a lot for this, and found several similar questions, but none quite address what I'm trying to accomplish.</p>
  
  <p>I'm trying to write a code that will search a PHP (multi)multidimensional array to see which subarray contains the unique key value that I have. Then I would like it to return the value of a different key in that same object subarray.</p>
  
  <pre><code>$Arraytosearch = Array(
  .
  //various other subarrays
  .
  [fields] =&gt; Array (
      .
      .
      .
      //I don't know the number of the object subarray
  
     [x] =&gt; PodioTextItemField Object (  
        [__attributes] =&gt; Array ( 
          [field_id] =&gt; 37325091 
          [type] =&gt; text 
          [external_id] =&gt; id 
          [label] =&gt; ID 
          [values] =&gt; Array ( 
              [0] =&gt; Array ( 
                  [value] =&gt; REF100019 ) ) 
  
          [config] =&gt; Array ( 
              [description] =&gt; [settings] =&gt; Array ( 
                  [size] =&gt; small ) 
              [required] =&gt; [mapping] =&gt; [label] =&gt; ID [visible] =&gt; 1 [delta] =&gt; 2 ) )
      .
      .
      .
       //(and so on)
  </code></pre>
  
  <p>I'd like to write a function that will return "REF100019" by supplying the value of the field_id => 37325091.</p>
  
  <p>Some things I have tried that I couldn't get to work:
  <code>foreach</code>
  and <code>new RecursiveIterator</code> although the tutorials I read weren't useful for my case here.</p>
  
  <p>Even though the array looks complicated, I think it will be easy since I already have the field id of the parent array.</p>
  
  <p>Thank you in advance for any guidance or sample codes that will work!</p>
  
  <p>Background: This is a part of the response I get from Podio after submitting a request to their API. I just don't know how to take that response and get the piece I need (the ID) so that I can echo it for users).</p>
  
  <p>EDIT: Thank you Orangepill and Barmar for the support. I tried your code. But was getting an error, which made me realize I hadn't given you the full array. I figured out how to get the Podio response to display in a more readable format (I was reading the full JSON response before from the Podio debug file which was super confusing), and figured out the full array is actually structured as I have shown below. </p>
  
  <p>I then took your code and was able to figure out how to make it work for my scenario (see below). Very proud of myself considering I have never written any code before, but I couldn't have done it without your help! Thanks again!</p>
  
  <pre><code>$Arraytosearch = Array(
    [items] =&gt; Array(
         [0] =&gt; PodioItem Object(
                [_attributes] =&gt; Array(
                        [fields] =&gt; Array (
                              [x] =&gt; PodioTextItemField Object (  
                                    [__attributes] =&gt; Array( 
                                           [field_id] =&gt; 37325091 
                                           [values] =&gt; Array( 
                                                  [0] =&gt; Array( 
                                                       [value] =&gt; REF100019 ) ) 
  </code></pre>
  
  <p>Note: For anyone who is new to programming like myself and wants the Podio response (or any JSON string) to display in a "pretty" readable format like above, use the code (from <a href="https://stackoverflow.com/questions/5393085/display-an-array-in-a-readable-format">Display an array in a readable/hierarchical format</a> thanks to Phenex):</p>
  
  <pre><code>print "&lt;pre&gt;";
  print_r($Arraytoformat);
  print "&lt;/pre&gt;";
  </code></pre>
  
  <p>And finally, the full code I used (using Orangepill's answer below) which searches the objects and arrays and gives me what I have been searching for for days now is as follows:</p>
  
  <pre><code>$Arraytosearch = PodioItem::filter(APP_ID, $filterParams);
  $fieldID = 37325091;    
  
      function getFirstValueByFieldId($fieldId, $Arraytosearch){
        foreach($Arraytosearch["items"][0]-&gt;__attributes["fields"] as $textitem){
          if ($textitem-&gt;__attributes["field_id"] == $fieldId){
          return $textitem-&gt;__attributes["values"][0]["value"];
      }}}
  
  $refID = getFirstValueByFieldId($fieldID, $Arraytosearch);
  </code></pre>