...

View Full Version : Search array fvalues for partial match



Hayyel
05-11-2009, 02:30 AM
Hello,

I am in need of a way to search an array for a value that contains a word.


$colorList = array("first name","last name","complete name","address","white");
$find = array_search("last", $colorList);
echo $find // results in $find being empty.

I need it to answer like the whole value would:


$colorList = array("first name","last name","complete name","address","white");
$find = array_search("last name", $cololorList);
echo $find // 1

So I need a partial match of the value to return the key and a way count the keys containing the partial value.

kbluhm
05-11-2009, 03:26 AM
http://www.php.net/preg_grep

Hayyel
05-11-2009, 11:37 AM
I knew there was a way..Great! Thanks.

Hayyel
05-11-2009, 09:36 PM
So preg_grep returns an array of keys containing the specified text. How do I then search that array if a specific key exists? in_array seems to return false even though the array contains the value.

//search $start for values containing "minimum"
$second = preg_grep('#(Minimum)#i', $start);
//search $second for $value and return true or false
$value = 10;
if (in_array($value, $second)) {
echo "Yes!!!";
} else {
echo "No!!!";}

$second contains:

Array
(
[0] => 10
[1] => 27
)
Array
(
[0] => 10
[1] => 28
)
Array
(
[0] => 11
[1] => 30
)
Array
(
[0] => 10
[1] => 26
)

Always returns False and I do not understand why.

timgolding
05-11-2009, 09:41 PM
Im wondering if its because the value is a string. Not sure though but maybe try



if (in_array((string)$value, $second)) {
echo "Yes!!!";
} else {
echo "No!!!";}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum