# How to echo or print an array in PHP?

18549

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](https://stackoverflow.com/questions/tagged/php) [arrays](https://stackoverflow.com/questions/tagged/arrays)[share](https://stackoverflow.com/q/9816889)[improve this question](https://stackoverflow.com/posts/9816889/edit)[edited May 31 '19 at 23:48](https://stackoverflow.com/posts/9816889/revisions)[![](https://i.stack.imgur.com/Z0g8T.png?s=32\&g=1)](https://stackoverflow.com/users/5411817/sherylhohman)[SherylHohman](https://stackoverflow.com/users/5411817/sherylhohman)8,7201111 gold badges5252 silver badges6666 bronze badgesasked Mar 22 '12 at 5:22[![](https://i.stack.imgur.com/9bURh.jpg?s=32\&g=1)](https://stackoverflow.com/users/807325/enexoonoma)[EnexoOnoma](https://stackoverflow.com/users/807325/enexoonoma)6,8281616 gold badges7474 silver badges154154 bronze badges[add a comment](https://stackoverflow.com/questions/9816889/how-to-echo-or-print-an-array-in-php#)

### 11 Answers

[active](https://stackoverflow.com/questions/9816889/how-to-echo-or-print-an-array-in-php?answertab=active#tab-top)[oldest](https://stackoverflow.com/questions/9816889/how-to-echo-or-print-an-array-in-php?answertab=oldest#tab-top)[votes](https://stackoverflow.com/questions/9816889/how-to-echo-or-print-an-array-in-php?answertab=votes#tab-top)111

This will do

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

[share](https://stackoverflow.com/a/9816938)[improve this answer](https://stackoverflow.com/posts/9816938/edit)answered Mar 22 '12 at 5:29[![](https://www.gravatar.com/avatar/e841b2deaeef854e0ab8ec6cdd1ba740?s=32\&d=identicon\&r=PG)](https://stackoverflow.com/users/376535/shiplu-mokaddim)[Shiplu Mokaddim](https://stackoverflow.com/users/376535/shiplu-mokaddim)49.1k1212 gold badges114114 silver badges174174 bronze badges

* Thank you for this. Can you tell me how can I echo for example only the type of the Array \[1] ? – [EnexoOnoma](https://stackoverflow.com/users/807325/enexoonoma) [Mar 22 '12 at 5:29](https://stackoverflow.com/questions/9816889/how-to-echo-or-print-an-array-in-php#comment12505844_9816938)
* 6\@Kaoukkos `echo $results['data'][1]['type'];` – [Shiplu Mokaddim](https://stackoverflow.com/users/376535/shiplu-mokaddim) [Mar 22 '12 at 5:33](https://stackoverflow.com/questions/9816889/how-to-echo-or-print-an-array-in-php#comment12505890_9816938)
* What if I dont have any key like `data`? – [Pratik Butani](https://stackoverflow.com/users/1318946/pratik-butani) [Oct 24 '18 at 11:10](https://stackoverflow.com/questions/9816889/how-to-echo-or-print-an-array-in-php#comment92841155_9816938)

[add a comment](https://stackoverflow.com/questions/9816889/how-to-echo-or-print-an-array-in-php#)441

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>[share](https://stackoverflow.com/a/9816958)[improve this answer](https://stackoverflow.com/posts/9816958/edit)[edited Feb 4 '16 at 17:34](https://stackoverflow.com/posts/9816958/revisions)[![](https://www.gravatar.com/avatar/58f9884421d1af250a816ac3eaaa5a13?s=32\&d=identicon\&r=PG)](https://stackoverflow.com/users/1811992/web-tiki)[web-tiki](https://stackoverflow.com/users/1811992/web-tiki)79.8k2525 gold badges182182 silver badges218218 bronze badgesanswered Mar 22 '12 at 5:32[![](https://i.stack.imgur.com/t9zJy.png?s=32\&g=1)](https://stackoverflow.com/users/396476/ibrahim-azhar-armar)[Ibrahim Azhar Armar](https://stackoverflow.com/users/396476/ibrahim-azhar-armar)21.3k2929 gold badges106106 silver badges179179 bronze badges

* 1Nice and elegant. You might want to change the closing tag in #1 from \<pre/> to \</pre>. – [Michael](https://stackoverflow.com/users/244549/michael) [Jun 21 '14 at 21:47](https://stackoverflow.com/questions/9816889/how-to-echo-or-print-an-array-in-php#comment37640303_9816958)
* 1This answer does not address the mistake that OP did. – [Shiplu Mokaddim](https://stackoverflow.com/users/376535/shiplu-mokaddim) [Nov 21 '14 at 17:28](https://stackoverflow.com/questions/9816889/how-to-echo-or-print-an-array-in-php#comment42647349_9816958)
* 2could you explain how the \<pre>\</pre> construct makes this display "nicely?" – [Robin Andrews](https://stackoverflow.com/users/3042018/robin-andrews) [May 28 '16 at 17:04](https://stackoverflow.com/questions/9816889/how-to-echo-or-print-an-array-in-php#comment62498140_9816958)
* 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](https://stackoverflow.com/users/2619679/j-c) [Sep 29 '16 at 9:00](https://stackoverflow.com/questions/9816889/how-to-echo-or-print-an-array-in-php#comment66826370_9816958)

[add a comment](https://stackoverflow.com/questions/9816889/how-to-echo-or-print-an-array-in-php#)91

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.[share](https://stackoverflow.com/a/17371991)[improve this answer](https://stackoverflow.com/posts/17371991/edit)[edited Jan 7 '14 at 2:07](https://stackoverflow.com/posts/17371991/revisions)answered Jun 28 '13 at 19:11[![](https://i.stack.imgur.com/GcrmP.jpg?s=32\&g=1)](https://stackoverflow.com/users/1362366/mark-e)[Mark E](https://stackoverflow.com/users/1362366/mark-e)2,6351515 silver badges3131 bronze badges

* 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 – [Hamid Araghi](https://stackoverflow.com/users/4411896/hamid-araghi) [Mar 29 '19 at 11:42](https://stackoverflow.com/questions/9816889/how-to-echo-or-print-an-array-in-php#comment97552237_17371991)

[add a comment](https://stackoverflow.com/questions/9816889/how-to-echo-or-print-an-array-in-php#)29

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

### [`print_r()`](http://php.net/manual/en/function.print-r.php)

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()`](http://php.net/manual/en/function.var-dump.php)

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()`](http://php.net/manual/en/function.var-export.php)

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](https://stackoverflow.com/a/588362/5104748)) 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`](http://php.net/manual/en/function.echo.php)

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 
```

[share](https://stackoverflow.com/a/52801002)[improve this answer](https://stackoverflow.com/posts/52801002/edit)answered Oct 14 '18 at 8:51[![](https://www.gravatar.com/avatar/a3147dbf27b3d0950d7c9d964d11045e?s=32\&d=identicon\&r=PG\&f=1)](https://stackoverflow.com/users/5104748/mohammad)[Mohammad](https://stackoverflow.com/users/5104748/mohammad)17.7k1313 gold badges4343 silver badges6868 bronze badges[add a comment](https://stackoverflow.com/questions/9816889/how-to-echo-or-print-an-array-in-php#)20

Did you try using [`print_r`](http://php.net/manual/en/function.print-r.php) to print it in human-readable form?[share](https://stackoverflow.com/a/9816921)[improve this answer](https://stackoverflow.com/posts/9816921/edit)[edited Jul 15 '15 at 14:50](https://stackoverflow.com/posts/9816921/revisions)[![](https://www.gravatar.com/avatar/f5cfb52c6e63da167d1dc8ac8befa427?s=32\&d=identicon\&r=PG\&f=1)](https://stackoverflow.com/users/1980250/kamal-pal)[kamal pal](https://stackoverflow.com/users/1980250/kamal-pal)4,05955 gold badges2020 silver badges3939 bronze badgesanswered Mar 22 '12 at 5:26[![](https://www.gravatar.com/avatar/bb3f31cfad3f609744da4b4ca3224f88?s=32\&d=identicon\&r=PG)](https://stackoverflow.com/users/108561/walker)[Walker](https://stackoverflow.com/users/108561/walker)1,14522 gold badges1212 silver badges2626 bronze badges[add a comment](https://stackoverflow.com/questions/9816889/how-to-echo-or-print-an-array-in-php#)19

You can use [`print_r`](https://www.php.net/manual/en/function.print-r.php), [`var_dump`](https://www.php.net/manual/en/function.var-dump.php) and [`var_export`](https://www.php.net/manual/en/function.var-export.php) 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>';
}
```

[share](https://stackoverflow.com/a/32021234)[improve this answer](https://stackoverflow.com/posts/32021234/edit)[edited Aug 21 '19 at 8:23](https://stackoverflow.com/posts/32021234/revisions)answered Aug 15 '15 at 3:30[![](https://i.stack.imgur.com/DU96V.jpg?s=32\&g=1)](https://stackoverflow.com/users/11956084/ankur-tiwari)[Ankur Tiwari](https://stackoverflow.com/users/11956084/ankur-tiwari)2,40622 gold badges1313 silver badges3838 bronze badges[add a comment](https://stackoverflow.com/questions/9816889/how-to-echo-or-print-an-array-in-php#)17

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

or `echo $results['data'][1]['type'];`[share](https://stackoverflow.com/a/9816907)[improve this answer](https://stackoverflow.com/posts/9816907/edit)[edited Mar 22 '12 at 5:47](https://stackoverflow.com/posts/9816907/revisions)answered Mar 22 '12 at 5:25[![](https://www.gravatar.com/avatar/f3634195974b1c5e78fe970ffd6b76f7?s=32\&d=identicon\&r=PG)](https://stackoverflow.com/users/1173667/rezigned)[Rezigned](https://stackoverflow.com/users/1173667/rezigned)4,11211 gold badge1616 silver badges1717 bronze badges

* Thank you for this. Can you tell me how can I echo for example only the type of the Array \[1] ? – [EnexoOnoma](https://stackoverflow.com/users/807325/enexoonoma) [Mar 22 '12 at 5:29](https://stackoverflow.com/questions/9816889/how-to-echo-or-print-an-array-in-php#comment12505841_9816907)
* what do you mean by Array \[1]? :o – [Andreas Wong](https://stackoverflow.com/users/135448/andreas-wong) [Mar 22 '12 at 5:32](https://stackoverflow.com/questions/9816889/how-to-echo-or-print-an-array-in-php#comment12505873_9816907)
* @andreas I want to echo only the COMMUNITY or the 163703342377960. Not the entire content of the array but specifically – [EnexoOnoma](https://stackoverflow.com/users/807325/enexoonoma) [Mar 22 '12 at 5:35](https://stackoverflow.com/questions/9816889/how-to-echo-or-print-an-array-in-php#comment12505903_9816907)
* Try `echo $results['data'][1]['page_id'];` – [Andreas Wong](https://stackoverflow.com/users/135448/andreas-wong) [Mar 22 '12 at 5:36](https://stackoverflow.com/questions/9816889/how-to-echo-or-print-an-array-in-php#comment12505918_9816907)

[add a comment](https://stackoverflow.com/questions/9816889/how-to-echo-or-print-an-array-in-php#)8

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>";
?>
```

[share](https://stackoverflow.com/a/31430967)[improve this answer](https://stackoverflow.com/posts/31430967/edit)[edited Aug 2 '17 at 20:30](https://stackoverflow.com/posts/31430967/revisions)[![](https://i.stack.imgur.com/IU0E8.jpg?s=32\&g=1)](https://stackoverflow.com/users/6509507/blackcoat77)[Blackcoat77](https://stackoverflow.com/users/6509507/blackcoat77)1,32411 gold badge1414 silver badges2525 bronze badgesanswered Jul 15 '15 at 12:53[![](https://www.gravatar.com/avatar/05bc06071ed6c39aa500aab3f7c175cf?s=32\&d=identicon\&r=PG\&f=1)](https://stackoverflow.com/users/5048091/vinod-kirte)[Vinod Kirte](https://stackoverflow.com/users/5048091/vinod-kirte)18111 silver badge66 bronze badges[add a comment](https://stackoverflow.com/questions/9816889/how-to-echo-or-print-an-array-in-php#)3

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

[share](https://stackoverflow.com/a/41445803)[improve this answer](https://stackoverflow.com/posts/41445803/edit)answered Jan 3 '17 at 14:31[![](https://www.gravatar.com/avatar/9b5fe7a66b12bbccd7910228a347f78a?s=32\&d=identicon\&r=PG)](https://stackoverflow.com/users/411222/niclas)[Niclas](https://stackoverflow.com/users/411222/niclas)1,20633 gold badges1414 silver badges2525 bronze badges[add a comment](https://stackoverflow.com/questions/9816889/how-to-echo-or-print-an-array-in-php#)0

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.

I hope this helps[share](https://stackoverflow.com/a/56950880)[improve this answer](https://stackoverflow.com/posts/56950880/edit)answered Jul 9 '19 at 10:48[![](https://www.gravatar.com/avatar/af9e2baa06be7725bfe424202d875a80?s=32\&d=identicon\&r=PG)](https://stackoverflow.com/users/1915577/heider-sati)[Heider Sati](https://stackoverflow.com/users/1915577/heider-sati)1,4481313 silver badges2020 bronze badges[add a comment](https://stackoverflow.com/questions/9816889/how-to-echo-or-print-an-array-in-php#)-2

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

[share](https://stackoverflow.com/a/50017889)[improve this answer](https://stackoverflow.com/posts/50017889/edit)answered Apr 25 '18 at 8:50[![](https://www.gravatar.com/avatar/fd9fa3ceaa75faa22c089e9b38b5c860?s=32\&d=identicon\&r=PG\&f=1)](https://stackoverflow.com/users/9412353/a-morales)[A. Morales](https://stackoverflow.com/users/9412353/a-morales)651212 bronze badges[add a comment](https://stackoverflow.com/questions/9816889/how-to-echo-or-print-an-array-in-php#)[**Highly active question**](https://stackoverflow.com/help/privileges/protect-questions). Earn 10 reputation in order to answer this question. The reputation requirement helps protect this question from spam and non-answer activity.

### Not the answer you're looking for? Browse other questions tagged [php](https://stackoverflow.com/questions/tagged/php) [arrays](https://stackoverflow.com/questions/tagged/arrays) or [ask your own question](https://stackoverflow.com/questions/ask).


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://learnphp.gitbook.io/learnphp/how-to-echo-or-print-an-array-in-php.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
