Finding Substring in Array

All,

Found this old thread at:

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

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).

C,

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

Thanks!

OMR

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);

  1.   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));