如何打印或回显 PHP 中的数组索引

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

How to print or echo the array index of in PHP

phparraysprintingecho

提问by Ali

I'm trying to complete my assignment and this is the last thing to do now.

我正在努力完成我的任务,这是现在要做的最后一件事。

I know if I want to print the whole array I can just use foreachand many different method to print the entire array

我知道如果我想打印整个数组,我可以使用foreach许多不同的方法来打印整个数组

foreach($v as $k=>$variable_name) { echo "<p> This is index of $k. value is $variable_name <br/></p>";}

But what if I want to only print each index separately?

但是如果我只想单独打印每个索引怎么办?

I want to do the error message under each form so that is why I want each of them separate.

我想在每个表单下显示错误消息,这就是为什么我希望每个表单都分开。

I tried with $v[0]and nothing appear.

我试过,$v[0]没有出现。

is there a trick or something am I missing?

有什么技巧或我错过了什么吗?

采纳答案by Damien Pirsy

If you're talking about an associative array, you need to fetch the index directly:

如果您在谈论关联数组,则需要直接获取索引:

Example:

例子:

$array = array ('test' => 'value1', 'test2' => 'value2');   
echo $array['test']; // value1

You can do a print_r($array) to see the array structure nicely formatted:

您可以执行 print_r($array) 以查看格式良好的数组结构:

<pre>
<?php print_r($array);?>
</pre>

What you're doing instead is fetch a value by its numerical index, like in

相反,您正在做的是通过其数字索引获取值,例如

$array = array('test','test2','test3');
echo $array[0];  // test

On a further note, you can check beforehand if a key exists using array_key_exists():

进一步说明,您可以使用array_key_exists()预先检查键是否存在:

var_dump(array_key_exists('test2',$array));  // (bool) TRUE

回答by Somnath Muluk

array_keys()will print indexes in array.

array_keys()将打印数组中的索引。

print_r(array_keys($arr));

回答by Xunnamius

I believe you're looking for this: http://php.net/manual/en/function.array-keys.php

我相信你正在寻找这个:http: //php.net/manual/en/function.array-keys.php

Try (from the above page):

尝试(从上面的页面):

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

  $array = array("blue", "red", "green", "blue", "blue");
  print_r(array_keys($array, "blue"));

  $array = array("color" => array("blue", "red", "green"),
               "size"  => array("small", "medium", "large"));
  print_r(array_keys($array));
?>

回答by Drew

If you're using the foreachloop then you're probably using an associative array (i.e. $v['inputName']) So, using $v[0]wouldn't work because the indexes aren't defined by numbers - they are defined by lettering. You could use a foreachloop to then associate all the values to the numbered indexes and then us it like that.

如果您正在使用foreach循环,那么您可能正在使用关联数组(即$v['inputName'])因此,使用$v[0]将不起作用,因为索引不是由数字定义的——它们是由字母定义的。您可以使用foreach循环将所有值关联到编号索引,然后像这样使用它。

$x = array();
foreach($v as $key=>$value) {

   $x[count($x)] = $key;

}

echo $x[0];

In that case $x[0]would work

在那种情况下$x[0]会起作用

回答by Nishu Tayal

You can use php array_keysfunction to get all keys. If Its Associative Array,

您可以使用 php array_keys函数来获取所有键。如果它的关联数组,

$array = array("color" => array("blue", "red", "green"),
               "size"  => array("small", "medium", "large"));
print_r(array_keys($array));

output will be :

输出将是:

Array
(
    [0] => color
    [1] => size
)

Other way is :

其他方式是:

if(is_array($v){
    foreach($v as $k=>$value) { 
         echo "<br/>". $k ;  // $k is the key 
    }
}

回答by blearn

use print_rfunction to print the array :

使用print_r函数打印数组:

print_r($array)