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)) {

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:

    [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?



This line:


should be

return $this->arraySearch($value,$needle,$depth);

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.