[RESOLVED] Searching array values - working just needs a tweak !
Results 1 to 8 of 8

Thread: [RESOLVED] Searching array values - working just needs a tweak !

  1. #1
    Senior Member
    Join Date
    Mar 2009
    Posts
    160

    resolved [RESOLVED] Searching array values - working just needs a tweak !

    Hi
    I'm using the following to filter results from specific lines of a text file.

    But I have one slight issue.. The example below shows the issue :

    PHP Code:
    <?php

         $line 
    "1=\"Device\",3=\"User\",7=\"ID123456\",27=\"Node\"";
         
    $q explode(",",str_replace('"','',$line));
         
    $p = (search_array($q"7=")); echo "ID : ".$p;

         
         
    function 
    stipos($haystack$needle){
        return 
    strpos($haystackstristr$haystack$needle ));
    }

    function 
    search_array $array$term )
        {
           foreach ( 
    $array as $key => $value )
                if ( 
    stipos$value$term ) !== false )
                 
    $val str_replace('"',"",preg_replace("/[a-zA-Z0-9]=/","",$array[$key]));
                   if (isset(
    $val)) return $val;
              return 
    false;
        } 
      
    ?>
    I'm search for 7= and then expect to return the value associated to that. In this example, I'd expect : ID123456

    However I have an additional entry in $line that is 27=\"Node\"
    So the result I actually get is 2Node, not ID123456

    I only want a match on 7= and NOT 17= or 27= which it's currently doing.
    How do I change the to only return the value against 7= and not a mix of 27= & it's value ??

    Thanks

  2. #2
    Pna lbh ernq guvf¿
    Join Date
    Jul 2004
    Location
    Kansas City area
    Posts
    19,429
    As for your issue, you'd want to make sure the preceding character is either a comma or the beginning-of-string indicator (e.g. '^' in PCREs).

    However, a bigger issue might be: Why aren't you using a DBMS (e.g. MySQL) for this? At the very least, why not use SQLite to manage your database?

  3. #3
    Senior Member
    Join Date
    Mar 2009
    Posts
    160
    Thanks for the reply.
    I get a plain flat file and then read it in, I'm only selecting a few values from it so a DB is a bit of overkill.

    I've tried updating this to :
    PHP Code:
         $p = (search_array($q",7=")); echo "ID : ".$p
    ie: adding a comma before the 7= but this results in no values being returned.

    Any ideas ?

  4. #4
    High Energy Magic Dept. NogDog's Avatar
    Join Date
    Aug 2006
    Location
    Ankh-Morpork
    Posts
    13,943
    A somewhat different approach:
    PHP Code:
    <?php

    $line 
    "1=\"Device\",3=\"User\",7=\"ID123456\",27=\"Node\"";

    $value_7 null;
    if(
    preg_match('#\b7="([^"]*)"#'$line$matches)) {
        
    $value_7 $matches[1];
    }
    var_dump($value_7);
    Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be." ~ from Nation, by Terry Pratchett

    "But the main reason that any programmer learning any new language thinks the new language is SO much better than the old one is because he’s a better programmer now!" ~ http://www.oreillynet.com/ruby/blog/...ck_to_p_1.html


    eBookworm.us

  5. #5
    Senior Member
    Join Date
    Mar 2009
    Posts
    160
    NogDog thanks.
    Please can you explain this ?

  6. #6
    Pna lbh ernq guvf¿
    Join Date
    Jul 2004
    Location
    Kansas City area
    Posts
    19,429
    Quote Originally Posted by Mad_T View Post
    I get a plain flat file and then read it in, I'm only selecting a few values from it so a DB is a bit of overkill.
    Well you already have a database - I was simply offering that using a DBMS might be a more efficient way of managing that database.

    Regardless, the method I had in mind above is more or less exactly what NogDog came up with, although you could also have utilized the fact that it appears you're being given "key=value" pairs (e.g. the value on the left of the equal sign will never be repeated within a row - it's a unique identifier). Example:

    PHP Code:
    // I'm assuming you actually have a file with many lines like the example string:
    $file = [
        
    '1="Device",3="User",7="ID1",27="Node"',
        
    '1="Device",3="User",7="ID2",27="Node"',
        
    '1="Device",3="User",7="ID3",27="Node"',
        
    '1="Device",3="User",7="ID4",27="Node"'
    ];

    // ... and then a loop to process all of them:
    foreach($file as $line) {
        if(
    preg_match_all('/(?:,|)([^=]+)="([^"]+)"(?:,|)/'$line$data)) {
            
    $data array_combine($data[1], $data[2]);
            
            print 
    "Found row with ID: $data[7]!\n";
        }

    And of course the loop could be modified to instead store the data for later use if you didn't want to (or are unable to) process the data immediately.
    Last edited by bradgrafelman; 02-13-2013 at 12:20 PM.

  7. #7
    High Energy Magic Dept. NogDog's Avatar
    Join Date
    Aug 2006
    Location
    Ankh-Morpork
    Posts
    13,943
    Quote Originally Posted by Mad_T View Post
    NogDog thanks.
    Please can you explain this ?
    The \b sequence in a PCRE regex is a "word boundary" assertion, so \b7 will match on any "7" preceded by a white-space or any non-word character (I think the underscore is considered a word character in this case, but I'd have to check to be sure). The [^"]* is matching on 0 to n characters that are not a double quote, and wrapping that expression in parentheses makes it a sub-pattern that can be separately selected from the resulting $matches array.
    Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be." ~ from Nation, by Terry Pratchett

    "But the main reason that any programmer learning any new language thinks the new language is SO much better than the old one is because he’s a better programmer now!" ~ http://www.oreillynet.com/ruby/blog/...ck_to_p_1.html


    eBookworm.us

  8. #8
    Senior Member
    Join Date
    Mar 2009
    Posts
    160
    Thanks NogDog, that works perfectly for what I need.

    bradgrafelman - thanks I can see a use for this in another script I need.

    Cheers

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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