[英]printing from an array php
Trying to print an integer from an array in php. 尝试从php中的数组打印整数。
I am attempting a question on the coding site hackerrank . 我正在编码站点hackerrank上尝试提问。
The pre-supplied function takes in a string input and manipulates it into an array like so: 预先提供的函数接受一个字符串输入,并将其处理成一个数组,如下所示:
<?php
$handle = fopen ("php://stdin","r");
fscanf($handle,"%d",$n);
$a = array();
for($a_i = 0; $a_i < $n; $a_i++) {
$a_temp = fgets($handle);
$a[] = explode(" ",$a_temp);
array_walk($a[$a_i],'intval');
}
echo $a[0];
?>
However running 但是跑步
echo $a[0]
results the following error: 导致以下错误:
PHP Notice: Array to string conversion in solution.php on line 12
and outputs: 和输出:
Array
I understand that this is because I am trying to manipulate the array as a string but I am not sure how else one could print out indexed values. 我了解这是因为我试图将数组作为字符串进行操作,但是我不确定别人还能如何打印出索引值。 Ideally I would like to use a c-type for loop: 理想情况下,我想使用c类型的循环:
for($x = 0; $x < $n; $x++) {
echo $a[$x];
}
Using print_r($a[0]) gives the following output: 使用print_r($ a [0])给出以下输出:
For printing an array use print_r()
. 要打印数组,请使用print_r()
。
Example: print_r($a[0])
; 示例: print_r($a[0])
;
$a[0]
is an array in this scope and not a string. $a[0]
是此范围内的数组,而不是字符串。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.