简体   繁体   English

打印阵列的键

[英]Print the keys of an array

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(). 函数内部的代码返回一个警告:为foreach()提供的参数无效。 How can I pull the keys out of the array? 如何将键拉出阵列?

You can use PHP's array_keys function 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 foreach which allows you to separate the key and value for every element, like so: 或者,您可以使用特殊的foreach运行数组,该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: 此外,请确保使用"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>";

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). 或者,您可以传入stdClass的实例(将参数强制转换为对象)。 But an array does the job. 但阵列完成了这项工作。

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; 

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. 您可以使用varargs将可变数量的参数传递给函数。 Here's an example: 这是一个例子:

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. 我只是给你一些选择。

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

尝试这个

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

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)));
<?php
$array = array(0 => 100, "color" => "red");
print_r(array_keys($array));
?>
foreach(array_keys($parameters) as $key) {
   echo $key.'<br/>';
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM