SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast
    Join Date
    Jan 2008
    Posts
    57
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Finding Substring in Array

    All,


    Found this old thread at:


    http://www.sitepoint.com/forums/showthread.php?609826


    Tinkered with it enough to know the REGEX part does not work, but gives "Array to string" conversion errors.


    Anyway I need to do this on arrays where the value is 3 to 4 levels deep and the key must be accurate, so nesting and/or recalling of the function is necessary if the array value from "foreach ($myarray as $key => $value) is itself an array.


    My search must work for searching substrings of the values similar to the SQL ==> field LIKE '%value%' <==.


    In particular, I need to find today's date in a datetime field that MySQL has put into an array.


    I need the Keys for this and or each occurance so I can accurately pull the data I need to push into another array being written to and Excel report.


    All help appreciated!


    Thanks!


    OMR
    Last edited by OldManRiver; Dec 13, 2011 at 12:35. Reason: Lost Data and Formatting

  2. #2
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Are you asking this because you cannot influence Mysql to select matching dates in the database?

    If you are then you might want to show us the sql statements which are generating these arrays.

    If you are not, then you could show some example arrays - exact examples of what you have got - and what you want to end up with (obfuscate or remove anything which might be commercially valuable or too revealing of course).

  3. #3
    SitePoint Enthusiast
    Join Date
    Jan 2008
    Posts
    57
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    C,

    No control over the MySQL, just get this dump and have to massage it.

    Thanks!

    OMR

  4. #4
    Non-Member
    Join Date
    Nov 2010
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This can be accomplished by two ways:

    1. Using regular expressions (preg_grep). like

    $lines = array('I have a cat.', 'My dog is blue.', 'One cute hamster.', 'Dogs are cool');
    $matches = preg_grep('/\bdogs?\b/i', $lines);
    $keys = array_keys($matches);

    print_r($matches);


    2. Using foreach with its key/value pair followed by the string method stripos. Like

    $my_array = array('one' => 'I have a cat.', 'two' => 'My dog is blue.', 'three' => 'One cute hamster.');
    $search_term = "dog";

    function search_array ( array $array, $term )
    {
    foreach ( $array as $key => $value )
    if ( stripos( $value, $term ) !== false )
    return $key;

    return false;
    }

    print_r(search_array($my_array, $search_term));


Bookmarks

Posting Permissions

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