1

I have an array that has "formname" in it as a $key. When I execute the following function:

    function in_array_r($needle, $arr, $strict = true) {
    $form_id = $lead['form_id'];
                $user_id = $lead['id'];
                $attachments = array();
$arr=get_defined_vars();
$needle="formna1me";
    foreach ($arr as $item) {
        if (($strict ? $item === $needle : $item == $needle) || (is_array($item) && in_array_r($needle, $item, $strict))) {
            echo "found"; exit;
        }
    }

    echo "notfound"; exit;
}

It returns "found" as it should. But if I change the $needle to $needle = "bbrubrcuyrfbur" it also returns found. It is simply always returning found? Not sure what is wrong.

hakre
  • 178,314
  • 47
  • 389
  • 754
Chris
  • 165
  • 1
  • 3
  • 14

2 Answers2

1

You are calling the function recursively. Even when you call the function with needle as bbrubrcuyrfbur, in the if condition the function is called recursively with needle as formna1me.

Inside the first recursion, $arr=get_defined_vars(); will read the value of $needle as formna1me. Then $needle will be reassigned formna1me and the if condition will match formna1me from $needle with the one in $args.

Lines 2 to 6 should probably not be in that function.

air4x
  • 5,384
  • 1
  • 21
  • 36
0

is_array supposed to work like below you are checking the item in is_array instead of array

$yes = array('this', 'is', 'an array');

echo is_array($yes) ? 'Array' : 'not an Array';

what is_array is doing is that

is_array — Finds whether a variable is an array

as your comment

tofind that the value is in array try in_array — Checks if a value exists in an array

$arr = array("Mac", "NT", "msc", "Linux");
if (in_array("Linux", $arr)) {
   echo 'yes it is';
}
NullPoiиteя
  • 53,430
  • 22
  • 120
  • 137