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?

4

3 Answers 3

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();
?>
Sign up to request clarification or add additional context in comments.

4 Comments

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.
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?
For that you need to normalise your array or tell me what will be the conditions to fetch that value..
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"
                )
        )
        )
    )
)

4 Comments

Editing array is not a option in this case. I need to work with array as it is.
try this: echo $this->in_array_r($v[0][0], $v) ? 'found' : 'not found';
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.
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.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.