0

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);
2
  • why not try something like foreach($arrays as $array) { end($array); $key = key($array); var_dump $key; } Commented Jan 16, 2020 at 10:17
  • you have typo on count($item['child']>0) - the ) in the wrong place - it should be count($item['child'])>0 Commented Jan 16, 2020 at 10:26

1 Answer 1

0

Not sure this is what you're looking for, but it might help you solve your issue(s).

Function pathFinder() is a recursive function, i.e. it will call itself as many times as needed to traverse your subject array.

Just set the needle, i.e. the key you're looking for, and it returns all paths to the key in your subject array.

An example of its use:

We have an array $arr with three keys called one, we're going to use the function to find the paths to all three keys:

$arr = [
    ['one' => 'once', 
     'two' => ['one' => 'twice', 'two' => 'twice', ['one' => 'thrice']], 
     'three' => 'once',]
];

$needle = 'one'; // the key we're looking for
$result = pathFinder($arr, $needle); // invoke function

/**
 * function pathFinder() returns the path(s) to a key in an array.
 *
 * @param array  $arr    the subject array
 * @param string $needle the key we're looking for
 *
 * @return array            the paths leading to the key we're looking for
 */
function pathFinder(array $arr = [], $needle = ''): array
{
    static $path = '';
    static $paths = [];

    foreach ($arr as $key => $value) {
        if (is_array($value)) {
            $path .= $key . "->";
            pathFinder($value, $needle);
        } else {
            if ($key === $needle) {
                $paths[] = $path . $key; // store path
            }
        }
    }
    return $paths; // return all found paths to key $needle
}

echo '<pre>';
print_r($result);
echo '</pre>';

output:

Array
(
    [0] => 0->one
    [1] => 0->two->one
    [2] => 0->two->0->one
)

working demo

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.