Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 05-11-2009, 02:30 AM   PM User | #1
Hayyel
New Coder

 
Join Date: Jan 2009
Posts: 91
Thanks: 17
Thanked 1 Time in 1 Post
Hayyel is an unknown quantity at this point
Search array fvalues for partial match

Hello,

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

PHP Code:
$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:

PHP Code:
$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.
Hayyel is offline   Reply With Quote
Old 05-11-2009, 03:26 AM   PM User | #2
kbluhm
Senior Coder

 
kbluhm's Avatar
 
Join Date: Apr 2007
Location: Philadelphia, PA, USA
Posts: 1,502
Thanks: 2
Thanked 258 Times in 254 Posts
kbluhm will become famous soon enough
http://www.php.net/preg_grep
__________________
ZCE
kbluhm is offline   Reply With Quote
Users who have thanked kbluhm for this post:
Hayyel (05-11-2009)
Old 05-11-2009, 11:37 AM   PM User | #3
Hayyel
New Coder

 
Join Date: Jan 2009
Posts: 91
Thanks: 17
Thanked 1 Time in 1 Post
Hayyel is an unknown quantity at this point
I knew there was a way..Great! Thanks.
Hayyel is offline   Reply With Quote
Old 05-11-2009, 09:36 PM   PM User | #4
Hayyel
New Coder

 
Join Date: Jan 2009
Posts: 91
Thanks: 17
Thanked 1 Time in 1 Post
Hayyel is an unknown quantity at this point
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.
PHP Code:
//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:
PHP Code:
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.
Hayyel is offline   Reply With Quote
Old 05-11-2009, 09:41 PM   PM User | #5
timgolding
Senior Coder

 
timgolding's Avatar
 
Join Date: Aug 2006
Location: Southampton
Posts: 1,460
Thanks: 89
Thanked 110 Times in 109 Posts
timgolding is on a distinguished road
Im wondering if its because the value is a string. Not sure though but maybe try

PHP Code:
if (in_array((string)$value$second)) {
    echo 
"Yes!!!";
    } else {
    echo 
"No!!!";} 
__________________
You can not say you know how to do something, until you can teach it to someone else.
timgolding is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:54 AM.


Advertisement
Log in to turn off these ads.