Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Jan 2009
    Posts
    91
    Thanks
    17
    Thanked 1 Time in 1 Post

    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.

  • #2
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts

  • Users who have thanked kbluhm for this post:

    Hayyel (05-11-2009)

  • #3
    New Coder
    Join Date
    Jan 2009
    Posts
    91
    Thanks
    17
    Thanked 1 Time in 1 Post
    I knew there was a way..Great! Thanks.

  • #4
    New Coder
    Join Date
    Jan 2009
    Posts
    91
    Thanks
    17
    Thanked 1 Time in 1 Post
    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.

  • #5
    Senior Coder timgolding's Avatar
    Join Date
    Aug 2006
    Location
    Southampton
    Posts
    1,517
    Thanks
    114
    Thanked 110 Times in 109 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •