简体   繁体   中英

printing from an array php

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:

hackerrank输出

For printing an array use print_r() .

Example: print_r($a[0]) ;

$a[0] is an array in this scope and not a string.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM