1
過去幾個小時一直在爲此付出努力。試圖爲數組構建一個搜索函數,然後吐出一個包含所有具有關鍵字的數組的新函數。搜索多維數組以獲得值並返回新數組
這是我正在使用的函數,不知道這是否是正確的方向,它是返回數組中的數組,這不是我想要的。
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
(
[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
)
)
我想發生這樣的是使用數據,然後發現有輸入的關鍵字的任何陣列。比如搜索「條例」將返回一個新的數組和含有「條例」的任何其他陣列結構是這樣的:
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
)
)
我讀過關於這裏是一些其他職位,做類似的事情,尤其是這一個:How to search by key=>value in a multidimensional array in PHP這似乎很接近,除了我只想在字符串中搜索一個值而不是鍵。任何幫助是極大的讚賞。