How to echo or print an array in PHP?
https://stackoverflow.com/questions/9816889/how-to-echo-or-print-an-array-in-php
18549
I have this array
My question is how can I just echo the content without this structure? I tried
11 Answers
This will do
To see the contents of array you can use.
1) print_r($array);
or if you want nicely formatted array then:
2) use var_dump($array)
to get more information of the content in the array like datatype and length.
If you just want to know the content without a format (e.g. for debuging purpose) I use this:
There are multiple function to printing array content that each has features.
Prints human-readable information about a variable.
Displays structured information about expressions that includes its type and value.
Displays structured information about the given variable that returned representation is valid PHP code.
Also there is another way to printing array content with certain conditions.
Output one or more strings. So if you want to print array content using echo
, you need to loop through array and in loop use echo
to printing array items.
print_r
: Convert into human readble form
var_dump()
: will show you the type of the thing as well as what's in it.
foreach loop
: using for each loop you can iterate each and every value of an array.
You have no need to put for loop to see the data into the array, you can simply do in following manner
I know this is an old question but if you want a parseable PHP representation you could use:
If you echo the exported code to a file.php (with a return statement) you may require it as
I checked the answer however, (for each) in PHP is deprecated and no longer work with the latest php versions.
Usually we would convert an array into a string to log it somewhere, perhaps debugging or test etc.
I would convert the array into a string by doing:
$Output = implied (",",$SourceArray);
Whereas:
$output is the result (where the string would be generated
",": is the separator (between each array field
$SourceArray: is your source array.
Loop through and print all the values of an associative array, you could use a foreach
loop, like this:
Last updated
Was this helpful?