问题描述
我正在尝试完成我的任务,这是现在要做的最后一件事.
I'm trying to complete my assignment and this is the last thing to do now.
我知道我是否要打印整个数组,我可以使用foreach
和许多不同的方法来打印整个数组
I know if I want to print the whole array I can just use foreach
and many different method to print the entire array
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.
我尝试使用$v[0]
,但没有任何显示.
I tried with $v[0]
and nothing appear.
有什么花招或我想念的东西吗?
is there a trick or something am I missing?
推荐答案
如果您正在谈论关联数组,则需要直接获取索引:
If you're talking about an associative array, you need to fetch the index directly:
示例:
$array = array ('test' => 'value1', 'test2' => 'value2');
echo $array['test']; // value1
您可以执行print_r($ array)来查看格式正确的数组结构:
You can do a print_r($array) to see the array structure nicely formatted:
<pre>
<?php print_r($array);?>
</pre>
您正在做的是通过其数字索引来获取值,例如in
What you're doing instead is fetch a value by its numerical index, like in
$array = array('test','test2','test3');
echo $array[0]; // test
请注意,您可以使用 array_key_exists():
var_dump(array_key_exists('test2',$array)); // (bool) TRUE
这篇关于如何在PHP中打印或回显数组的索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!