1

I'm quite new to PHP... I am trying to check if a value exists in my $Session array and then display the content according to that but it is always showing 'Match not found' even when I know the value does exist in the array.

This is a print_r of my array:

Array (
    [0] => Array ( 
        [0] => SIEMENS 
        [1] => 523-096-32 
        [2] => 1
        [3] => New 
    ) 
)

When I do:

$array = $_SESSION['arr'];

if( in_array( "523-096-32" , $array ) ){
    echo "Match found";
}else{
    echo "Match not found";
}

The result is "Match not found".

I have also tried replacing in_array with array_key_exists but I'm having the same result.

My question is: where am I going wrong and how can I fix it to display "Match found"? Any help would be greatly appreciated.

executable
  • 2,788
  • 3
  • 16
  • 39
Nick M
  • 53
  • 7
  • 8
    `in_array` only checks one level deep, your value is in a nested array so you'd need to do `in_array( "523-096-32" , $array[0] )` to find it – naththedeveloper Nov 30 '18 at 15:41
  • 1
    Possible duplicate of [in\_array() and multidimensional array](https://stackoverflow.com/questions/4128323/in-array-and-multidimensional-array) – user3783243 Nov 30 '18 at 15:49
  • 2
    array_key_exists is for key not for value. [documentation](http://php.net/manual/en/function.array-key-exists.php) – Alberto Moro Nov 30 '18 at 15:52

1 Answers1

-1

Using in_array("523-096-32", $array[0]) as the comment on your question indicates will work, but it may not be the best solution depending on what $_SESSION['arr'] is supposed to do. The best way to check for that value depends on whether or not $_SESSION['arr'] is supposed to be a multidimensional array.

It looks like the array inside $_SESSION['arr'] is a set of values representing a single item. If $_SESSION['arr'] is meant to hold a group of those items, although there's only one in the example you showed, looking in $array[0] will only check the first item. Instead you can iterate it and check each item for that value until you find it (or not).

$found = false;
foreach ($_SESSION['arr'] as $item) {
    if ($item[1] === "523-096-32") {
        $found = true;
        break;
    }
}
echo $found ? 'Match found' : 'Match not found';

If the items inside $_SESSION['arr'] aren't consistently structured, referring to $item[1] won't be accurate and you should use in_array instead.

if (in_array("523-096-32", $item)) { ...

If $_SESSION['arr'] is only one level deep, you shouldn't need to use a recursive function to find a match.


On the other hand, if $_SESSION['arr'] is only meant to be one array, rather than an array with another array in it like you have, you probably assigned it incorrectly, like

$_SESSION['arr'][] = ['SIEMENS', '523-096-32', '1', 'New'];

instead of

$_SESSION['arr'] = ['SIEMENS', '523-096-32', '1', 'New'];

If you assign it directly like the second example here, using in_array like you tried to do will work.

Don't Panic
  • 37,589
  • 9
  • 55
  • 71
  • Hmm, whoever downvoted, is there something incorrect or inaccurate in the answer, or did you just think the question should be closed instead of answered? If it's the second thing I can understand why you wouldn't want to say anything, but if not I'd like to know if I made a mistake. – Don't Panic Nov 30 '18 at 16:54
  • No offense, but don't see how this could help OP. You are replacing the nice and short in_array() with a inflexible foreach-loop, pointing to a fixed array index. This kinda make the simple "bug" more complicated than it is, doesn't it? Cheers! (I'm the downvoter, sorry, slow in writing... ;)) So, to be "constructive": I would stick with the in_array solution and maybe offer an recursive function. This would help in most possible cases. – n.r. Nov 30 '18 at 16:55
  • No offense taken, thanks for commenting. I just thought that based on the values in the inner array it seemed likely that `$_SESSION['arr'][1]` etc. could also exist at some point, and if that was the case, a single `in_array` would no longer suffice. And if that _wasn't_ the case, it shouldn't have been multidimensional in the first place and that should be fixed instead of looking at the pointless `[0]`. Then again, I do have a tendency to to overthink things :-) – Don't Panic Nov 30 '18 at 17:02
  • 1
    But I'm not arguing with your vote. I appreciate that there are different opinions on things. – Don't Panic Nov 30 '18 at 17:04