如何输出这些PHP数组值?

时间:2010-01-04 04:31:25

标签: php arrays

3 个答案:

答案 0 :(得分:3)

看起来像print_r()输出给我。

答案 1 :(得分:1)

使用foreach语句迭代遍历每个回显的数组

foreach($data as $k => $v) {
  echo "{$k} => {$v}";
}

$ data是输入数据 $ k是关键$ v是$ value

以上仅适用于数组数组的单个深度数组,例如示例数据,只要您需要使用递归函数,然后检查该值是否为数组,如果这样,则以递归方式调用该函数。

如果数据结构永远不会改变,那么一些嵌套循环将完成这项工作。 (有些人认为递归是邪恶的,我可怜他们)

DC

答案 2 :(得分:0)

从我们的数组

开始
$myarray = array(
  "Key1" => "This is the value for Key1",
  "Key2" => "And this is the value for Key2"
);

显示所有输出(有助于调试)

print_r($myarray);
/* or */ 
var_dump($myarray);

仅显示单个值

print $myarray["Key1"]; // Prints 'This is the value for Key1'
print $myarray[0]; // Prints 'This is the value for Key1'