[英]Search value in multidimension array and keep keys association
I have this array converted from xml of webservice. 我从Web服务的xml转换此数组。 I have 500 items in this array.
我有500个项目在这个数组中。 I want to search any value and return all found array items with key association ( similar to database select query ).
我想搜索任何值并返回具有键关联的所有找到的数组项(类似于数据库select查询)。 So if I search 'dummy' then it should return first item of this array.
因此,如果我搜索“虚拟”,则它应返回此数组的第一项。
Array ( [12.12.2014] => Array ( [7] => Array ( [id] => 1672 [date] => 12.12.2014 [description] => rummy dummy data [room] => delux [Type] => garden [from] => 17:00 [to] => 17:45 [assets] => Array ( [asset] => Array ( [0] => Array ( [number] => 5275 [detail] => primary one ) [1] => Array ( [number] => 19 [detail] => secondary one ) ) ) [references] => Array ( [reference] => Array ( [personnumber] => 479470 [type] => worker [name] => David [department] => Sales [cv] => Array ( [pdetails] => follow later ) [profile] => True ) ) ) ) [13.12.2014] => Array ( [17] => Array ( [id] => 1672 [date] => 13.12.2014 [description] => brown fox jump [room] => star [Type] => city [from] => 17:00 [to] => 17:45 [assets] => Array ( [asset] => Array ( [number] => 5275 [detail] => prime two ) ) [references] => Array ( [reference] => Array ( [personnumber] => 479470 [type] => manager [name] => Albert [department] => Purchase [cv] => Array ( [pdetails] => follow later ) [profile] => True ) ) ) ) )
I tried stripos to search string in array value and in_array based functions but either it gives incorrect result or key association is not maintained. 我尝试了stripos在基于数组值和基于in_array的函数中搜索字符串,但是它给出的结果不正确或键关联未得到维护。
I am unable to find a way to maintain key->value. 我找不到保持键->值的方法。
function search($array, $key, $value) { $results = array(); if (is_array($array)) { if (isset($array[$key]) && $array[$key] == $value) $results[] = $array; foreach ($array as $subarray) $results = array_merge($results, search($subarray, $key, $value)); } return $results; }
This may be worst function you have ever seen but this do the job. 这可能是您见过的最糟糕的功能,但是可以完成任务。 If some one can make it recursive ( array may be further deeper ).
如果有人可以使其递归(数组可能会更深)。
function search_in_multi_array($srchvalue, $array) { $foundkey = ''; if (is_array($array) && count($array) > 0) { foreach($array as $pkey => $pvalue) { foreach($pvalue as $ckey => $cvalue) { if (is_array($cvalue) && count($cvalue) > 0) { if(in_array($srchvalue,$cvalue)) { $foundkey[$pkey][$ckey] = $cvalue; } foreach($cvalue as $dkey => $dvalue) { if(!is_array($dvalue)) { $pos = stripos($dvalue, $srchvalue); if ($pos !== false) { $foundkey[$pkey][$ckey] = $cvalue; } } } } } } } return $foundkey; }
Function call - $needle = 'fox'; 函数调用-$ needle ='fox'; search_in_multi_array($needle, $my_array);
search_in_multi_array($ needle,$ my_array); This is the output
这是输出
Array ( [13.12.2014] => Array ( [17] => Array ( [id] => 1672 [date] => 13.12.2014 [description] => brown fox jump [room] => star [Type1] => city [from] => 17:00 [to] => 17:45 [assets] => Array ( [asset] => Array ( [number] => 5275 [detail] => prime two ) ) [references] => Array ( [reference] => Array ( [personnumber] => 479470 [Type1] => manager [name] => Albert [department] => Purchase [cv] => Array ( [pdetails] => follow later ) [profile] => 1 ) ) ) ) )
You may want to have a look at XPath to run queries against the "raw" XML, see DOMXPath for example. 您可能需要查看XPath才能针对“原始” XML运行查询,例如,请参阅DOMXPath 。
Something like /myRootTag/someDayTag[.//text()[contains(.,'MY_SEARCH_VALUE')]]
should do the trick, selecting and returning all the someDayTag
XML elements below the myRootTag
which have the text MY_SEARCH_VALUE
in any child node. 喜欢的东西
/myRootTag/someDayTag[.//text()[contains(.,'MY_SEARCH_VALUE')]]
应该做的伎俩,选择并返回所有someDayTag
下面的XML元素myRootTag
具有文本MY_SEARCH_VALUE
任何子节点。
You can use array_search() function to look through the array values. 您可以使用array_search()函数浏览数组值。 But array_search only looks in single-dimensional array.
但是array_search只看一维数组。
Since you have a multi-dimensional array, you can write custom recursive function to search recursively in the array 由于您具有多维数组,因此您可以编写自定义递归函数以在数组中递归搜索
function recursive_array_search($needle,$haystack) {
foreach($haystack as $key=>$value) {
$current_key=$key;
if($needle===$value OR (is_array($value) && recursive_array_search($needle,$value) !== false)) {
return $current_key;
}
}
return false;
}
But please note, using array_search() is a easier approach but not a optimised function. 但是请注意,使用array_search()是更简单的方法,但不是优化的函数。 Which means if you're more concerned about your memory utilisation then I would also addtionally suggest.
这意味着,如果您更担心内存使用率,那么我也建议您。
$dictonary[<key-searched>] = array('12.12.2014', '13.12.2014')
$dictonary[<key-searched>] = array('12.12.2014', '13.12.2014')
Its always easier to search by key-value than to search in a multi-dimensional array. 通过键值进行搜索总是比在多维数组中进行搜索更容易。
I've made a simple routine that will extract the values in the array you're looking for: 我做了一个简单的例程,它将提取您要查找的数组中的值:
function search_keys($needle,$haystack)
{
foreach($haystack as $key => $value)
{
if (is_array($value)) $output[$key] = search_keys($needle,$value);
else if (strpos($value,$needle) !== FALSE) $output[$key] = $value;
}
return isset($output) ? $output : NULL; // prevent warning
}
echo '<pre>';
print_r(search_keys('garden',$data));
echo '</pre>';
This will return 'garden' as: 这将返回“花园”为:
Array
(
[12.12.2014] => Array
(
[7] => Array
(
[Type] => garden
)
)
)
You can further process the output of this function, or change the function, as needed. 您可以根据需要进一步处理此功能的输出,或更改功能。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.