1

I am trying to find value in multidimensional array using recursive in_array function, but I always get not found. This is the structure of array $v :

Array
(
    [0] => Array
        (
            [EV000005] => Array
                (
                    [0] => Array
                        (
                            [0] => Array
                                (
                                    [0] => EN
                                    [1] => Thread holding
                                )

                            [1] => Array
                                (
                                    [0] => nl-NL
                                    [1] => Schroefdraadhouder
                                )
                        )
                )
        )

This is function:

public function in_array_r($needle, $haystack, $strict = false) {
            foreach ($haystack as $item) {
                if (($strict ? $item === $needle : $item == $needle) || (is_array($item) && $this->in_array_r($needle, $item, $strict))) {
                    return true;
                }
            }
            return false;
        }

And this is how I call function which echoes to me not found instead of found:

echo $this->in_array_r("EV000005", $v) ? 'found' : 'not found';

I also tried to use $strict = true but same result. Why is the search for EV000005 unsuccessful in this case?

wp78de
  • 18,207
  • 7
  • 43
  • 71
punky
  • 125
  • 2
  • 12
  • Would this topic help? https://stackoverflow.com/questions/1019076/how-to-search-by-key-value-in-a-multidimensional-array-in-php – ino Dec 29 '17 at 07:30
  • try this ["EV000005"] => Array(...) instead of [EV000005] => Array(...) – maddy23285 Dec 29 '17 at 07:32
  • 1
    You use function that find value in multidimentional array, but you need array_key_exists extended for such arrays such as https://hotexamples.com/examples/-/-/array_key_exists_recursive/php-array_key_exists_recursive-function-examples.html – splash58 Dec 29 '17 at 07:46
  • There is also this... https://stackoverflow.com/questions/20592046/find-key-in-nested-associative-array Which is another of a few options. – TimBrownlaw Dec 29 '17 at 08:10

3 Answers3

2

Reference from link.

You are searching recursively for value, but in example you are trying to search it by key.

You can use this function to achieve this,

function array_key_exists_custom($needle, $haystack)
{
    $result = array_key_exists($needle, $haystack);
    if ($result) {
        return $result;
    }

    foreach ($haystack as $v) {
        if (is_array($v)) {
            $result = array_key_exists_custom($needle, $v);
        }
        if ($result) {
            return $result;
        }

    }
    return $result;
}

Here is working demo.

Here is object oriented working demo as per requirement.

Here is your code :

<?php
//Enter your code here, enjoy!
class A
{

    public function array_key_exists_custom($needle, $haystack)
    {
        $result = array_key_exists($needle, $haystack);
        if ($result) {
            return $result;
        }

        foreach ($haystack as $v) {
            if (is_array($v)) {
                $result = $this->array_key_exists_custom($needle, $v);
            }
            if ($result) {
                return $result;
            }

        }
        return $result;
    }
    public function getData()
    {
        $arr = array
            (
            "0" => array
            (
                "EV000005" => array
                (
                    "0" => array
                    (
                        "0" => array
                        (
                            "0" => "EN",
                            "1" => "Thread holding",
                        ),

                        "1" => array
                        (
                            "0" => "nl-NL",
                            "1" => "Schroefdraadhouder",
                        ),
                    ),
                ),
            ),
        );
        $temp = $this->array_key_exists_custom("EV000005", $arr);
        echo ($temp ? "found" : "not found");
    }
}
$obj = new A;

$obj->getData();
?>
Rahul
  • 18,271
  • 7
  • 41
  • 60
  • I believe that it solution for my case, but I have following problem: Uncaught Error: Call to undefined function array_key_exists_custom(). I am using function in this way: $temp = $this->array_key_exists_custom("EV000005",$v); . Function is defined and called inside class classification and this is why I am using $this. I don't see reason for this error message. – punky Dec 29 '17 at 08:21
  • Could you help me little more? I need to check does EV000005 exist, and if it exist I want to collect his "Thread holding" value from structure, how should I do this? – punky Dec 29 '17 at 08:53
  • For that you need to normalise your array or tell me what will be the conditions to fetch that value.. – Rahul Dec 29 '17 at 09:15
  • Take a look on this : https://stackoverflow.com/questions/48020478/php-search-for-key-in-multidimenstional-array-and-get-its-values-deeper-from-t – punky Dec 29 '17 at 09:44
0

Try this, your array should be:

Array
(
[0] => Array
    (
    ["EV000005"] => Array
        (
        [0] => Array
        (
            [0] => Array
                (
                [0] => "EN"
                [1] => "Thread holding"
                )

            [1] => Array
                (
                [0] => "nl-NL"
                [1] => "Schroefdraadhouder"
                )
        )
        )
    )
)
maddy23285
  • 738
  • 6
  • 16
  • Editing array is not a option in this case. I need to work with array as it is. – punky Dec 29 '17 at 07:45
  • try this: echo $this->in_array_r($v[0][0], $v) ? 'found' : 'not found'; – maddy23285 Dec 29 '17 at 07:54
  • The sample array that shows missing quotes on the strings is just a result of the print_r() function. So it's a non issue. – TimBrownlaw Dec 29 '17 at 07:57
  • Just discovered var_export() using pre tags gives a different output. At least it quotes the stings. That is, once you have constructed the above array correctly. – TimBrownlaw Dec 29 '17 at 08:03
0

in_array_r searches for values, but you are looking vor a key. try this gist

Thomas L.
  • 104
  • 8