How to print an array in PHP

How to print an array in PHP

Introduction

An array is one kind of data structure, which can store multiple items of related data types. The printing of an array is one of the fundamental functions of PHP.

How to print an array in PHP?

You can print an array with the values in different ways using the var_dump() or print_r() or json_encode() or foreach loop functions. These functions can print the value of an array in the human-readable format or print the output value of the program array.

1) print_r() function:

print_r() function is one of the built-in functions in PHP used to print or show the contents of a variable. It typically prints a human-readable format about a variable and the value, if it is a string, integer, or float. If you provide an array, it can show the values in a format that includes keys and elements.

Syntax
print_r($variableName, $boolVariable)

The first parameter $variableName is a mandatory parameter, and another parameter $boolVariable is an optional variable set to false by default.

Example

Output

Array
(
[x] => Department
[y] => Employee
[z] => Salary
)

2) var_dump() function:

The var_dump() function is one of the built-in functions in PHP used to output the information about variables. This function performs the information regarding one or more expressions that include a type and value of an expression. An array will extend the values recursively that shows its structure through indentation.

Syntax
var_dump($variableName)

The above syntax takes only one parameter $variableName and returns the structured information of the variable.

Example

Output

array(5) {
[0]=> int(1)
[1]=> int(2)
[2]=> int(3)
[3]=> int(4)
[4]=>
array(4) {
[0]=>
string(5) “arpit”
[1]=>
string(4) “raja”
[2]=>
string(3) “jai”
[3]=>
string(7) “adithya”
}
}

3) json_encode() function:

json_encode() function can return a string that contains a JSON representation of value. This function is used to encode an array that results in a human-readable JSON string format in PHP.

Syntax
string json_encode( $value, $option, $depth )

$value is a mandatory parameter, which defines a value to be encoded.

Example

Output

{“Employee Name”:”Arpit”,”Employee Id”:”110″,”Address”:[“Street No 1″,”Madhapur”,”Hyderabad”]}

4) foreach() loop:

The foreach() loop can provide a simple way to access array indexes. In PHP, the foreach() loop only works for arrays or objects. If you are trying to use a variable with different data types, it will throw an error.

Syntax
foreach (array_expression as $value)

The above syntax is for foreach loop that will continue its iteration till the length of an array and assign the value of each element to $value variable. This variable can able to access inside a body of the foreach loop.

Example

Output

Welcome to Java2Blog
Java
PHP
Python
Scala
JavaScript

That’s all about how to print an array in PHP.


import_contacts

You may also like:

Related Posts

  • Check if String contains Substring in PHP
    25 October

    How to check if String contains substring in PHP

    Table of ContentsIntroductionHow to check if a string contains substring in PHP?1) strpos() function:2) strstr() function:3) stristr() function:4) preg_match() function: Introduction A string is a sequence of characters used as a literal constant or variable. The specific part of a string is known as a substring. In this article, we will see different ways to […]

  • How to initialize empty array in PHP
    18 October

    Declare empty array in php

    Table of ContentsUsing square brackets to declare empty array in PHPUsing array() to declare empty array in PHP An array is a data structure where you can store the same or different types of data in a single variable. In PHP, you can declare empty array using square brackets ([]) or using array() function. Using […]

Leave a Reply

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

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.