0

我有这个数组

$array = array(
    "one" => "bar",
    "two"    => "21",
    "three"    => "22",
    "four"    => "25",
    "five" => array(
         "xxxxxxxxxxxxxxxxxx" => array(
             "ar1" => "food",
             "ar2" => "dr",
             "ar3" => "ch",
             "ar4" => "ju"
         ),
        "yyyyyyyyyyyyyyyyyyyy" => array(
             "ar1" => "food",
             "ar2" => "dr",
             "ar3" => "ch",
             "ar4" => "ju"
         )),
    "six" => "et",
    "seven" => "op",
    "eight" => "hjs",
    "nine" => array(
         "1" => array(
             "ar5" => "food",
             "ar87" => "dr",
             "ar21" => "ch",
             "ar443" => "ju"
         ),
         "2" => array(
             "73" => "food",
             "82" => "dr",
             "90" => "ch",
             "2123" => "ju"
         )),
    "ten" => "bar",
    "eleven" => "bar",
    "twelve" => "bar"
);

并且正在像这样找到给定级别的所有数组键

foreach ($array['five'] as $keyed=> $user) {
foreach ($user as $key => $value) {
       echo "Key: $key; Value: $value Keyed: $keyed<br />\n";
}
}

但是,我想获取所有找到的数组键以供使用,array_key_exists所以我需要将找到的键转换为数组。我正在尝试通过拥有这个数组

function the_keys($val){
foreach ($val['five'] as $keyed=> $user) {
foreach ($user as $key => $value) {
    $keyed = array();
}
}
}

但即使这样也不会像我想的那样将找到的键转换为数组。我需要做什么才能将找到的键转换为数组?

4

2 回答 2

3

要从$val['five']您那里获取所有密钥,可以使用array_keys()

$keyed = array_keys($val['five']);

在这种情况下,您不需要循环。

于 2013-09-07T14:02:28.543 回答
0

也许这会解决你的问题

foreach($val['five'] as $arrFive) {
    //Here you have both arrays
    foreach($arrFive as $arrXXXX) {
        //Parse array with index xxxx
    }
    foreach($arrFive as $arrYYYY) {
        //Parse array with index yyyy
    }
}
于 2013-09-07T14:00:26.410 回答