How to echo or print an array in PHP?

PhpArrays

Php Problem Overview


I have this array

Array
(
  [data] => Array
    (
      [0] => Array
        (
          [page_id] => 204725966262837
          [type] => WEBSITE
        )

      [1] => Array
        (
          [page_id] => 163703342377960
          [type] => COMMUNITY
        )
      )
)

My question is how can I just echo the content without this structure? I tried

foreach ($results as $result) {
    echo $result->type; 
    echo "<br>";
} 

Php Solutions


Solution 1 - Php

To see the contents of array you can use.

1) print_r($array); or if you want nicely formatted array then:

echo '<pre>'; print_r($array); echo '</pre>';

2) use var_dump($array) to get more information of the content in the array like datatype and length.

3) you can loop the array using php's foreach(); and get the desired output. more info on foreach in php's documentation website:
http://in3.php.net/manual/en/control-structures.foreach.php

Solution 2 - Php

This will do

foreach($results['data'] as $result) {
    echo $result['type'], '<br>';
}

Solution 3 - Php

If you just want to know the content without a format (e.g. for debuging purpose) I use this:

echo json_encode($anArray);

This will show it as a JSON which is pretty human readable.

Solution 4 - Php

There are multiple function to printing array content that each has features.

Prints human-readable information about a variable.

$arr = ["a", "b", "c"];

echo "<pre>";
print_r($arr);
echo "</pre>";

Array
(
    [0] => a
    [1] => b
    [2] => c
)


var_dump()

Displays structured information about expressions that includes its type and value.

echo "<pre>";
var_dump($arr);
echo "</pre>";

array(3) {
  [0]=>
  string(1) "a"
  [1]=>
  string(1) "b"
  [2]=>
  string(1) "c"
}


var_export()

Displays structured information about the given variable that returned representation is valid PHP code.

echo "<pre>";
var_export($arr);
echo "</pre>";

array (
  0 => 'a',
  1 => 'b',
  2 => 'c',
)

Note that because browser condense multiple whitespace characters (including newlines) to a single space (answer) you need to wrap above functions in <pre></pre> to display result in correct format.


Also there is another way to printing array content with certain conditions.

echo

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.

foreach ($arr as $key=>$item){
    echo "$key => $item <br>";
}

0 => a 
1 => b 
2 => c 


Solution 5 - Php

You can use print_r, var_dump and var_export funcations of php:

print_r: Convert into human readble form

<?php
echo "<pre>";
 print_r($results); 
echo "</pre>";
?>

var_dump(): will show you the type of the thing as well as what's in it.

var_dump($results);

foreach loop: using for each loop you can iterate each and every value of an array.

foreach($results['data'] as $result) {
    echo $result['type'].'<br>';
}

Solution 6 - Php

Did you try using print_r to print it in human-readable form?

Solution 7 - Php

foreach($results['data'] as $result) {
    echo $result['type'], '<br />';
}

or echo $results['data'][1]['type'];

Solution 8 - Php

You have no need to put for loop to see the data into the array, you can simply do in following manner

<?php
echo "<pre>";
 print_r($results); 
echo "</pre>";
?>

Solution 9 - Php

Human readable: (eg. can be log to text file..)

print_r( $arr_name , TRUE);

Solution 10 - Php

You can use var_dump() function to display structured information about variables/expressions including its type and value, or you can use print_r() to display information about a variable in a way that's readable by humans.

Example: Say we have got the following array and we want to display its contents.

$arr = array ('xyz', false, true, 99, array('50'));
Array
(
    [0] => xyz
    [1] =>
    [2] => 1
    [3] => 99
    [4] => Array
        (
            [0] => 50
        )
)
var_dump() function - Displays values and types
array(5) {
  [0]=>
  string(3) "xyz"
  [1]=>
  bool(false)
  [2]=>
  bool(true)
  [3]=>
  int(100)
  [4]=>
  array(1) {
    [0]=>
    string(2) "50"
  }
}

The functions used in this answer can be found on the PHP.net website var_dump(), print_r()

For more details:

» https://stackhowto.com/how-to-display-php-variable-values-with-echo-print_r-and-var_dump/

» https://stackhowto.com/how-to-echo-an-array-in-php/

Solution 11 - Php

I know this is an old question but if you want a parseable PHP representation you could use:

$parseablePhpCode = var_export($yourVariable,true);

If you echo the exported code to a file.php (with a return statement) you may require it as

$yourVariable = require('file.php');

Solution 12 - Php

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 = implode(",", $SourceArray);

Whereas:

$output is the result (where the string would be generated

",": is the separator (between each array field

$SourceArray: is your source array.

I hope this helps

Solution 13 - Php

If you only need echo 'type' field, can use function 'array_column' like:

$arr = $your_array;
echo var_dump(array_column($arr['data'], 'type'));

Solution 14 - Php

Loop through and print all the values of an associative array, you could use a foreach loop, like this:

foreach($results as $x => $value) {
    echo $value;
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionEnexoOnomaView Question on Stackoverflow
Solution 1 - PhpIbrahim Azhar ArmarView Answer on Stackoverflow
Solution 2 - PhpShiplu MokaddimView Answer on Stackoverflow
Solution 3 - PhpMark EView Answer on Stackoverflow
Solution 4 - PhpMohammadView Answer on Stackoverflow
Solution 5 - PhpAnkur TiwariView Answer on Stackoverflow
Solution 6 - PhpWalkerView Answer on Stackoverflow
Solution 7 - PhpRezignedView Answer on Stackoverflow
Solution 8 - PhpVinod KirteView Answer on Stackoverflow
Solution 9 - PhpAkin ZemanView Answer on Stackoverflow
Solution 10 - PhpthomasView Answer on Stackoverflow
Solution 11 - PhpNiclasView Answer on Stackoverflow
Solution 12 - PhpHeider SatiView Answer on Stackoverflow
Solution 13 - PhpdavlemView Answer on Stackoverflow
Solution 14 - PhpA. MoralesView Answer on Stackoverflow