Reputation: 1527
Trying to print an integer from an array in php.
I am attempting a question on the coding site 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:
for($x = 0; $x < $n; $x++) {
echo $a[$x];
}
Using print_r($a[0]) gives the following output:
Upvotes: 1
Views: 201
Reputation: 7466
For printing an array use print_r()
.
Example: print_r($a[0])
;
$a[0]
is an array in this scope and not a string.
Upvotes: 1