多维数组可以包含单个元素或另一个数组。 如何遍历?

This has been racking my brain for a few days. I thought I had a solution but I am finding it difficult to come up with an always working bullet proof piece of code. I am traversing a multidimensional array. I have two functions, one that looks at a single row and one that looks at an array. I have two poetential configurations for an array.

First Example

[
    'Person' => [
        'Name' => 'John Smith'
    ]
]

(One single result for person, with only one name for that person.)

Second Example

[
    'Person' => [
        'Name' => [
            0 => 'John Smith',
            1 => 'John Denver'
        ]
    ]
]

(One single person, with multiple names for that person.)

Third Example

[
    'Person' => [
         0 => [
             'Name' => 'John Smith'
         ],
         1 => [
             'Name' => 'Jane Doe'
         ]
    ]
]

(Two people, each with one name only.)

Fourth Example

[
    'Person' => [
         0 => [
             'Name' => [
                 0 => 'John Smith',
                 1 => 'John Denver'
             ]
         ],
         1 => [
             'Name' => 'Jane Doe'
         ]
    ]
]

(Two people, the first one has two names, the second only has one.)

Hopefully this is understood what I am saying. When there is only 1 record for something, it will return the single record. If there is more than one record, there will be an array containing each of those records.

I need to transverse through the multidimensional array from beginning to end. I am trying to figure out exactly how to check if there is one single answer or multiple.

I have tried checking for array key names, checking if keys are integers vs strings and if something is an array or not.

Nothing is quite working out as I expect. Can anyone help provide a way to transverse and check using for example a foreach loop or a function?

If it helps, I have another array that contains the names of the key fields that it is trying to look for. That array would have a similar layout to this:

[
    'Person' => [
        'type' => 'array',
        'subfields' => [
             'Name' => [
                 'type' => 'string'
              ]
         ]
     ]
]

Any help would be greatly appreciated.

Edit

Since this wasn't clear. I am looking for how to make the parent key apply to the child value independently of whether or not the value is a single value, or if it is an array of multiple values. The key parent needs to apply to all children, unless there is another parent key (there could be in theory an unlimited number of children / parent keys underneath a parent key.

Final

I managed to figure this out. Thank you to everyone for your help.

If you're just trying to get a list of people, you can normalise your data with array_walk_recursive to return this to sanity as a list of names from either format.

function normaliseData($peopleData) {
    $people = []; 
    array_walk_recursive($peopleData, function($item, $key) use(&$people) {    
        $people[] = $item;        
    });
    return $people;
}
print_r(normaliseData($first)); // Array ( [0] => John Smith )
print_r(normaliseData($second)); // Array ( [0] => John Smith [1] => John Denver )
print_r(normaliseData($third)); // Array ( [0] => John Smith [1] => Jane Doe )
print_r(normaliseData($fourth)); // Array ( [0] => John Smith [1] => John Denver [2] => Jane Doe )

For this array_walk_recursive is the best option as the post of @JasonBoss works, but if want to do it manually from a function you can do it like this.

Try this code snippet here

<?php
ini_set('display_errors', 1);

$array1=[
    'Person' => [
        'Name' => 'John Smith'
    ]
];
$array2=[
    'Person' => [
        'Name' => [
            0 => 'John Smith',
            1 => 'John Denver'
        ]
    ]
];
$array3=[
    'Person' => [
         0 => [
             'Name' => 'John Smith'
         ],
         1 => [
             'Name' => 'Jane Doe'
         ]
    ]
];
$array4=[
    'Person' => [
         0 => [
             'Name' => [
                 0 => 'John Smith',
                 1 => 'John Denver'
             ]
         ],
         1 => [
             'Name' => 'Jane Doe'
         ]
    ]
];

$result=array();
getRecords($array1);
print_r($result);

$result=array();
getRecords($array2);
print_r($result);

$result=array();
getRecords($array3);
print_r($result);

$result=array();
getRecords($array4);
print_r($result);


function getRecords($array)
{
    global $result;
    foreach($array as $key => $value)
    {
        if(is_string($value))
        {
            $result["names"][]=$value;
        }
        elseif(is_array($array[$key]) && count($array[$key]))
        {
            getRecords($value);
        }
    }
}