Been struggling with this for the last few hours. Trying to build a search function for an array and then spit out a new one containing all arrays that have a keyword.
Here is the function I am working with, not sure if this is heading in the right direction or not, it's returning arrays within arrays which is not what I intend.
function search_array($array, $needle) {
$results = array();
$hasValue = false;
foreach ($array as $subarray) {
foreach($subarray as $value){
if(strpos($value,$needle) !== false)
$hasValue = true;
}
if($hasValue = true)
$results[] = $array;
}
return $results;
}
Array I'm searching is structured as follows:
Array
(
[0] => Array
(
[ID] => 27
[title] => Steve Jobs
[balance] => $147
[paid_1] => $49
[date_1] => 26 August, 2013
[paid_2] => $49
[date_2] => 26 August, 2013
[paid_3] => $49
[date_3] => 26 August, 2013
)
[1] => Array
(
[ID] => 26
[title] => Bill Gates
[balance] => $300
[paid_1] => $100
[date_1] => 25 August, 2013
[paid_2] => $100
[date_2] => 25 August, 2013
[paid_3] => $100
[date_3] => 25 August, 2013
)
)
What I would like to happen is use data entered from a search form and then find any arrays that have the keyword entered. For instance searching for "Bill" would return a new array and any other arrays containing "Bill" structured like this:
Array
(
[0] => Array
(
[ID] => 26
[title] => Bill Gates
[balance] => $300
[paid_1] => $100
[date_1] => 25 August, 2013
[paid_2] => $100
[date_2] => 25 August, 2013
[paid_3] => $100
[date_3] => 25 August, 2013
)
)
I've read some other posts on here, doing something similar, especially this one: How to search by key=>value in a multidimensional array in PHP which seems close, except I would like to only search by a value within a string and not the key as well. Any help is greatly appreciated.