I'm working on multidimensional arrays and find cases similar to this question. After I tried it turned out I had to have the id and unique value sought, so it was different from my case.
so my case goes like this: I have a multidimensional array like this:
Array
(
[0] => Array
(
[item] => null
[count] => 0
[child] => Array
(
[Dagadu Bocah] => Array
(
[item] => Dagadu Bocah
[count] => 47
[child] => Array
(
[HirukPikuk] => Array
(
[item] => HirukPikuk
[count] => 5
[child] => Array
(
[DGD] => Array
(
[item] => DGD
[count] => 1
[child] =>
)
)
)
[DGD] => Array
(
[item] => DGD
[count] => 5
[child] => Array
(
[Malioboroman] => Array
(
[item] => Malioboroman
[count] => 1
[child] =>
)
)
)
[Malioboroman] => Array
(
[item] => Malioboroman
[count] => 2
[child] =>
)
)
)
what I'm looking for is how to search for arrays, for example 'DGD' it will produce all indexes ending in array 'DGD'
DGD => Dagadu Bocah->HirukPikuk,
Dagadu Bocah;
Malioboroman=> Dagadu Bocah->DGD,
Dagadu Bocah;
what I've tried is like this, with the final result using implode:
public function searchRec($haystack, $needle, $pathId=Array(), $pathIndex=Array())
{
foreach($haystack as $index => $item) {
$pathId[] = $item['count'];
$pathIndex[] = $index;
if($item['item'] == $needle) {
$returnObject = new stdClass();
$returnObject->match = $item;
$returnObject->pathId = $pathId;
$returnObject->pathIndex = $pathIndex;
return $returnObject;
}
if(isset($item['child']) && count($item['child']>0)) {
$result =searchRec($item['child'], $needle, $pathId, $pathIndex);
if($result) {
return $result;
}
}
}
return false;
}
$result = searchRec($Array, "Some Text2");
echo implode(" -> ", $result->pathId);
count($item['child']>0)- the)in the wrong place - it should becount($item['child'])>0