arraySearchKey: search for key in array and return value or path set

search in an array in any format and returns value of key if found.
Either as set with path to key, or as flat with only values.
Optional possible to prefix with the search keys to group found entries
This commit is contained in:
Clemens Schwaighofer
2023-04-26 14:40:39 +09:00
parent 2b079ff836
commit e80b3b8dfd
3 changed files with 310 additions and 0 deletions

View File

@@ -149,6 +149,38 @@ function rec(string $pre, string $cur, array $node = [])
return $node;
}
$data = [
'image' => 'foo',
'element' => 'w-1',
'rotate' => 360,
'html' => [
'image' => 'bar',
'result_image' => 'baz',
'rule' => 'wrong'
],
[
'image' => 'large'
],
[
'nothing' => 'wrong'
],
'nest' => [
'nust' => [
'nist' => [
'foo' => 'bar',
'image' => 'long, long'
]
]
],
's' => [
'image' => 'path?'
],
];
$search = ['image', 'result_image', 'nothing', 'EMPTY'];
$result = ArrayHandler::arraySearchKey($data, $search);
print "ARRAYSEARCHKEY: Search: " . DgS::printAr($search) . ", Found: " . DgS::printAr($result) . "<br>";
// $test = [
// 'A' => [
// 'B' => [],