300

I have this array

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

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

How can I just echo the content without this structure?

I tried

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

14 Answers 14

606

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 the datatype and length.

  3. You can loop the array using php's foreach(); and get the desired output. More info on foreach is in PHP's documentation website: foreach

2
  • 1
    This answer does not address the mistake that OP did. Nov 21, 2014 at 17:28
  • 1
    @Robin <pre> tag displays new lines and tabulation as it's outputed by print_r(); without <pre> you would see a messy unformatted bounds of data. To see it formatted you should then view the html page source.
    – j.c
    Sep 29, 2016 at 9:00
157

This will do

foreach($results['data'] as $result) {
    echo $result['type'], '<br>';
}
5
  • Thank you for this. Can you tell me how can I echo for example only the type of the Array [1] ?
    – EnexoOnoma
    Mar 22, 2012 at 5:29
  • 7
    @Kaoukkos echo $results['data'][1]['type']; Mar 22, 2012 at 5:33
  • 2
    What if I dont have any key like data? Oct 24, 2018 at 11:10
  • @PratikButani use var_dump($array) to get more information of the content of the array Nov 3, 2020 at 1:30
  • how can I insert this information in a database? for example, if I have a table with a column named type and i want o insert [0]['type'] , [1]['type'] and so on?
    – Maduro
    Aug 9, 2021 at 2:42
143

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

echo json_encode($anArray);

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

2
  • I wanted to extract some data from a html - php document using Cordova InAppBrowser executeScript method, without json_encode($array) I could not achieve that! Thanks a lot @Mark E Mar 29, 2019 at 11:42
  • 2
    echo json_encode($downloads, JSON_PRETTY_PRINT); works even better
    – 8ctopus
    Dec 4, 2020 at 9:12
57

There are multiple functions for printing array content that each has features.

print_r()

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 the browser condenses multiple whitespace characters (including newlines) to a single space (answer), you need to wrap above functions in <pre></pre> to display result in thee correct format.


Also, there is another way to print 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 the array and in the loop use echo to print array items.

foreach ($arr as $key=>$item){
    echo "$key => $item <br>";
}
0 => a
1 => b
2 => c
35

You can use print_r, var_dump and var_export functions of PHP:

print_r: Convert into human-readable 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 a for each loop, you can iterate each and every value of an array.

foreach($results['data'] as $result) {
    echo $result['type'] . '<br>';
}
2
  • how to insert the result in a database?
    – Maduro
    Aug 9, 2021 at 2:43
  • tell me if I'm wrong. I'm basing a E.R.P. on directory structure. Jun 21, 2022 at 22:09
25

Try using print_r to print it in human-readable form.

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

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

4
  • Thank you for this. Can you tell me how can I echo for example only the type of the Array [1] ?
    – EnexoOnoma
    Mar 22, 2012 at 5:29
  • what do you mean by Array [1]? :o Mar 22, 2012 at 5:32
  • @andreas I want to echo only the COMMUNITY or the 163703342377960. Not the entire content of the array but specifically
    – EnexoOnoma
    Mar 22, 2012 at 5:35
  • Try echo $results['data'][1]['page_id']; Mar 22, 2012 at 5:36
8

You don’t have any need to use a for loop to see the data into the array. You can simply do it in the following manner:

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

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'));

print_r() function - Displays human-readable output

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 documentation website, var_dump() and print_r().

For more details:

8

Human-readable (for example, can be logged to a text file...):

print_r($arr_name, TRUE);
3

If you want a parsable 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');
0
3

I checked the answers, however, (for each) in PHP it is deprecated and no longer works with the latest PHP versions.

Usually, we would convert an array into a string to log it somewhere, perhaps debugging, 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.

1

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

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

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;
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.