Answers.Body: 
  
  <p>This line:</p>
  
  <pre class="lang-php prettyprint-override"><code>$this-&gt;arraySearch($value,$needle,$depth);
  </code></pre>
  
  <p>should be</p>
  
  <pre class="lang-php prettyprint-override"><code>return $this-&gt;arraySearch($value,$needle,$depth);
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-06-30T12:20:51.330Z
Answers.LastActivityDate: 2017-06-30T12:20:51.330Z
Answers.UserDisplayName: Brandon Kelly
Answers.Body: <p>Check out the ArrayHelper class that comes with Yii and is extended by Craft. It may very well do all that you need</p>
  
  <p>Hmmmm never mind that might be Craft 3</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-06-30T10:37:11.130Z
Answers.LastActivityDate: 2017-06-30T10:44:53.267Z
Answers.LastEditDate: 2017-06-30T10:44:53.267Z
Answers.UserDisplayName: andrew.welch
Body: <p>I've got a plugin where I've written a simple recursive array search to find an array within a nested/multidimensional array which contains a certain value.</p>
  
  <pre><code>private function arraySearch($haystack,$needle,$depth = 1)
  {
      foreach($haystack AS $key =&gt; $value) {
          if (is_array($value) &amp;&amp; array_key_exists('title',$value) &amp;&amp; strtolower($value['title']) == strtolower($needle)) {
              return $value;
          } elseif(is_array($value)) {
              $depth++;
              $this-&gt;arraySearch($value,$needle,$depth);
          }
      }
  }
  </code></pre>
  
  <p>With some logging within this function I can see that it works and returns at the correct time but when I call it in my plugin methods the response is empty.</p>
  
  <p>For example, if I wanted to get an array which contains a <code>title</code> key and the value for this is <code>venue</code>, I'd type: </p>
  
  <pre><code>$result = $this-&gt;arraySearch($array,'Venue');
  </code></pre>
  
  <p>Applied to the following array:</p>
  
  <pre><code>Array
  (
      [pages] =&gt; Array
          (
              [0] =&gt; Array
                  (
                      [elements] =&gt; Array
                          (
                              [0] =&gt; Array
                                  (
                                      [type] =&gt; text
                                      [isRequired] =&gt; 1
                                      [name] =&gt; question1
                                      [title] =&gt; Venue
                                  )
  
                              [1] =&gt; Array
  </code></pre>
  
  <p>So this should return the array <code>$array['pages'][0]['elements][0]</code> but $result` seems to be empty.</p>
  
  <p>Any suggestions?</p>
  
  <p>Cole</p>
  
Tags: <plugin-development><php><array><function>
Title: Recursive array search
ThreadText: # Recursive array search
  
  (Asked by: username_0 on 2017-06-29)
  
  I've got a plugin where I've written a simple recursive array search to find an array within a nested/multidimensional array which contains a certain value.
  
  ```
  private function arraySearch($haystack,$needle,$depth = 1)
  {
      foreach($haystack AS $key => $value) {
          if (is_array($value) && array_key_exists('title',$value) && strtolower($value['title']) == strtolower($needle)) {
              return $value;
          } elseif(is_array($value)) {
              $depth++;
              $this->arraySearch($value,$needle,$depth);
          }
      }
  }
  
  ```
  
  With some logging within this function I can see that it works and returns at the correct time but when I call it in my plugin methods the response is empty.
  
  For example, if I wanted to get an array which contains a `title` key and the value for this is `venue`, I'd type:
  
  ```
  $result = $this->arraySearch($array,'Venue');
  
  ```
  
  Applied to the following array:
  
  ```
  Array
  (
      [pages] => Array
          (
              [0] => Array
                  (
                      [elements] => Array
                          (
                              [0] => Array
                                  (
                                      [type] => text
                                      [isRequired] => 1
                                      [name] => question1
                                      [title] => Venue
                                  )
  
                              [1] => Array
  
  ```
  
  So this should return the array `$array['pages'][0]['elements][0]` but $result` seems to be empty.
  
  Any suggestions?
  
  username_0
  
  **Tags:** plugin-development, php, array, function
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2017-06-30. Score: 2 | Accepted answer
  
  This line:
  
  ```php
  $this->arraySearch($value,$needle,$depth);
  
  ```
  
  should be
  
  ```php
  return $this->arraySearch($value,$needle,$depth);
  
  ```
  
  
  ---
  
  ### Answer by username_2 on 2017-06-30. Score: 0
  
  Check out the ArrayHelper class that comes with Yii and is extended by Craft. It may very well do all that you need
  
  Hmmmm never mind that might be Craft 3
  
  
  ---
  
  