• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / PHP / php Print Array – How to Print All Elements in Array

php Print Array – How to Print All Elements in Array

March 31, 2022 Leave a Comment

When working in php, we can print an array easily. To print arrays, you can use loops, print_r(), var_dump(), or json_encode().

The first example here shows how to print all the elements in an array with a loop.

$array = ["This","is","an","array"];

foreach($array as $ele) {
    echo $ele . "\n";
}

//Output:
This
is
an
array

You can also print arrays with the php print_r() function.

$array = ["This","is","an","array"];

print_r($array);

//Output:
Array
(
    [0] => This
    [1] => is
    [2] => an
    [3] => array
)

Another way to print a php array and the elements of array is with the var_dump() function.

$array = ["This","is","an","array"];

var_dump($array);

//Output:
array(4) {
  [0]=>
  string(4) "This"
  [1]=>
  string(2) "is"
  [2]=>
  string(2) "an"
  [3]=>
  string(5) "array"
}

One last way you can print an array is with the json_encode().

$array = ["This","is","an","array"];

echo json_encode($array);

//Output:
["This","is","an","array"]

When working with arrays in php, it is useful to be able to print the elements of an array.

There are many ways we can print the elements of an array in php.

If you want to get only the elements of the array, and not the array, you can loop over each element of an array and print it with echo. To print the elements of an array with php, we can use foreach to loop and print the elements of an array.

$array = ["This","is","an","array"];

foreach($array as $ele) {
    echo $ele . "\n";
}

//Output:
This
is
an
array

Printing All Elements of Array in php with print_r() Function

Another way we can print an array in php is with the php print_r() function.

print_r() prints human-readable information about a variable.

So, if you have an array, you can pass it to print_r() and get a human-readable array which shows all of the array keys and array values.

Below is an example in php showing how to print an array with print_r().

$array = ["This","is","an","array"];

print_r($array);

//Output:
Array
(
    [0] => This
    [1] => is
    [2] => an
    [3] => array
)

How to Print Array Elements in php with var_dump() Function

Another way we can print an array in php is with the php var_dump() function.

var_dump() returns information about a variable that includes its type and value.

So, if you have an array, you can pass it to var_dump() and get a data structure which has the type and value of each of the elements of the array.

Below is an example in php showing how to print an array with var_dump().

$array = ["This","is","an","array"];

var_dump($array);

//Output:
array(4) {
  [0]=>
  string(4) "This"
  [1]=>
  string(2) "is"
  [2]=>
  string(2) "an"
  [3]=>
  string(5) "array"
}

Printing Arrays in php with json_encode() Function

One last way we can print an array in php is with the php json_encode() function.

json_encode() returns the JSON representation of a variable. json_encode() is similar to the JavaScript stringify() function.

So, if you have an array, you can pass it to json_encode() and get the JSON representation of the array.

Below is an example in php showing how to print an array with the help of json_encode().

$array = ["This","is","an","array"];

echo json_encode($array);

//Output:
["This","is","an","array"]

Hopefully this article has been useful for you to learn the many ways of how to print an array and print the elements of an array in your php code.

Other Articles You'll Also Like:

  • 1.  php array_walk() – Modify Elements in Array with Callback Function
  • 2.  Remove HTML Tags from String in php
  • 3.  Add Months to Date in php
  • 4.  In PHP isset Method is Used For Checking if Variable is Defined
  • 5.  How to Get Current System IP Address in php
  • 6.  Truncate Strings in php Using substr()
  • 7.  Get Current Month of Date in php
  • 8.  php unset – How to Destroy Variables in php
  • 9.  Get Last Day of Month in php
  • 10.  How to Get Session ID in php with session_id()

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy