php 打印数组的键

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3507381/
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 10:01:50  来源:igfitidea点击:

Print the keys of an array

phparrays

提问by JMC

I could not figure out how to pass a variable number of variables into a function. I thought passing in an array and using the array keys for the variables names could replace the need to pass extra variables into the function, and it worked (I'm sure there is a better way to accomplish this, suggestions welcome). However, I can't seem to get the keys out of the array inside the function.

我不知道如何将可变数量的变量传递给函数。我认为传入一个数组并使用数组键作为变量名称可以代替将额外变量传递给函数的需要,并且它起作用了(我确定有更好的方法来实现这一点,欢迎提出建议)。但是,我似乎无法从函数内部的数组中取出键。

The array:

数组:

  $parameters[day] = 1;
  $parameters[month] = 8;
  $parameters[year] = 2010; 

Inside the function:

函数内部:

foreach(key($parameters) as $key)
{
   print($key);
   print("<br>");
}

The code inside the function retuns a warning: Invalid argument supplied for foreach(). How can I pull the keys out of the array?

函数内的代码返回警告:为 foreach() 提供的参数无效。如何将键从数组中拉出?

回答by Garrett

You can use PHP's array_keysfunction to grab the keys, like so:

您可以使用 PHP 的array_keys函数来获取密钥,如下所示:

foreach(array_keys($parameters) as $paramName)
  echo $paramName . "<br>";

Or, you can run through the array using a special foreachwhich allows you to separate the key and value for every element, like so:

或者,您可以使用特殊的方法遍历数组foreach,它允许您将每个元素的键和值分开,如下所示:

foreach($parameters as $paramName => $value)
  echo $paramName . "<br>";

Also, make sure that you are using a "string"(with quotes) or integer (like 1337) as your key, like so:

另外,请确保您使用 a "string"(带引号)或整数(如1337)作为您的键,如下所示:

$parameters["day"] = 1;
$parameters["month"] = 8;
$parameters["year"] = 2010;

OR if you want to get fancier:

或者,如果你想变得更狂热:

$parameters = array(
  "day" => 1,
  "month" => 8,
  "year" => 2010
);

Your code should look like:

您的代码应如下所示:

$parameters = array(
  "day" => 1,
  "month" => 8,
  "year" => 2010
);
foreach($parameters as $paramName => $paramValue)
  echo $paramName . "<br>";

回答by MrWhite

Passing an associative array to a function is a reasonable way to pass in a variable number of parameters.

将关联数组传递给函数是传递可变数量参数的合理方法。

foreach ($parameters as $key => $value) {
  echo $key . ' = ' . $value . '<br>';
}

Alternatively you could pass in an instance of stdClass(casting the argument to an object). But an array does the job.

或者,您可以传入stdClass(将参数转换为对象)的实例。但是数组可以完成这项工作。

I assume your array keys aren't constants, in which case they should be quoted strings:

我假设您的数组键不是常量,在这种情况下,它们应该是带引号的字符串:

$parameters['day'] = 1;
$parameters['month'] = 8;
$parameters['year'] = 2010; 

回答by kunal

The array_keysfunction will return an array of all the array keys.

array_keys函数将返回所有的数组键的数组。

回答by Rob Hruska

I'm sure there is a better way to accomplish this, suggestions welcome

我相信有更好的方法来实现这一点,欢迎提出建议

Because you asked for alternate suggestions, here's one. You can use varargs to pass a variable number of arguments to a function. Here's an example:

因为您要求提供替代建议,所以这里有一个。您可以使用 varargs 将可变数量的参数传递给函数。下面是一个例子:

function my_function() {
    $numArgs = func_num_args();
    $args = func_get_args(); // an array of the arguments, in order
}

This doesn't offer you "named" arguments, but it does allow you variable numbers of arguments (like you claim you're trying to do in your question).

这不会为您提供“命名”参数,但它确实允许您使用可变数量的参数(就像您声称您在问题中尝试做的那样)。

Here are some relevant documentation pages:

以下是一些相关的文档页面:

However, that's not to say that your array-based approach is a bad one. In some ways it provides batter readability since you're explicitly mapping keys to values; maintainers reading your code will be better-able to understand what's being passed to the function. I'm just giving you some options.

但是,这并不是说您的基于数组的方法不好。在某些方面,它提供了更好的可读性,因为您明确地将键映射到值;阅读您的代码的维护人员将能够更好地理解传递给函数的内容。我只是给你一些选择。

回答by jordanstephens

try this

尝试这个

foreach($parameters as $key => $value)

回答by Alexander Goncharov

If you want just to output keys, you can do this without cycles:

如果您只想输出密钥,则无需循环即可执行此操作:

$parameters = ["day" => 1, "month" => 8, "year" => 2010];
echo  implode('<BR>', array_keys($parameters));

Or output keys with values:

或者输出带有值的键:

$parameters = ["day" => 1, "month" => 8, "year" => 2010];
echo implode('<BR>', array_map(function($v, $k){ return $k.'='.$v;}, $parameters, array_keys($parameters)));

回答by user423388

<?php
$array = array(0 => 100, "color" => "red");
print_r(array_keys($array));
?>

回答by Sergey Eremin

foreach(array_keys($parameters) as $key) {
   echo $key.'<br/>';
}