返回 PHP 数组名称

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/847701/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 00:04:19  来源:igfitidea点击:

Return PHP array name

phparrays

提问by James

Array ( [kanye] => Array ( [0] => Kanya [1] => Janaye [2] => Kayne [3] => Kane [4] => Kaye ) [wst] => Array ( [0] => ST [1] => St [2] => st [3] => EST [4] => West ) )

Array
(
    [0] => Kanya
    [1] => Janaye
    [2] => Kayne
    [3] => Kane
    [4] => Kaye
)
Array
(
    [0] => ST
    [1] => St
    [2] => st
    [3] => EST
    [4] => West
)

I've got those two arrays inside one array. The top array holds them both, then below is each one individually. When I am displaying the individual arrays, how do I echo their name?

我将这两个数组放在一个数组中。最上面的数组包含它们,然后下面是每个单独的。当我显示单个数组时,如何回显它们的名称?

So the first would be kanye, then list the contents, etc.

所以第一个是kanye,然后列出内容等。

Hope that makes sense. I know it will be a simple bit of code but it's stumping me.

希望这是有道理的。我知道这将是一段简单的代码,但它让我很难过。

回答by viam0Zah

You can use a foreachstatement to get the key value pair of the array:

可以使用foreach语句获取数组的键值对:

$outer_arr = array('kanye' => array('Kanya', 'Janaye', 'Kayne', 'Kane'));
foreach($outer_arr as $key => $val) {
    print($key); // "kanye"
    print_r($val); // Array ( [0] => Kanya [1] => Janaye [2] => Kayne [3] => Kane )
}

回答by nickf

If you just need to get the keys, you can use array_keys

如果您只需要获取密钥,则可以使用 array_keys

$myArray = array(
    "Kanye" => array("Kane", ...)
    "West" => array("Wst", ...)
);

print_r(array_keys($myArray));
/*
array (
    0 => Kanye
    1 => West
)
*/

回答by grantwparks

How about just print_r on the outer array?

只在外部数组上使用 print_r 怎么样?