Reputation: 1211
ANSWERED: THE FUNCTION WORKS AS I WANTED IT TO. I HAD A TYPO IN MY TEST PHP THAT MADE IT RETURN TRUE REGARDLESS.
I am using in_array, and I'm trying to use it in a way where it will only return true if it's an exact match of one of the objects in the array, not just if it's "in" the array. e.g.
1. $sample_array = array('123', '234', '345');
2. $sample_array = array('23', '34', '45');
in_array('23', $sample_array);
In the above example, I would only want version 2 to return true, because it has the exact string, '23'. Version 1 returns true as well though, because the array contains instances of the string '23'.
How would I get this to work so that only version 2 returns true? Am I even using the right function?
Upvotes: 0
Views: 1211
Reputation: 17977
Version 1 does not return true. The function works exactly as it should.
<?php
$sample_array1 = array('123', '234', '345');
$sample_array2 = array('23', '34', '45');
echo in_array('23', $sample_array1); // 0
echo in_array('23', $sample_array2); // 1
Upvotes: 2
Reputation: 54729
This function is not a regex function. It checks to see if that exact value is in the array, not something that is 'like' that value. Checking if '23' is in an array will not return true for '234'.
in_array("23", array("23")); // true
in_array("23", array(23)); // true
in_array("23", array("234")); // false (0)
Notice, however, that it will check against different castings.
Upvotes: 2