How do i echo an array in php?

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 "
";
print_r[$arr];
echo "
";
Array
[
    [0] => a
    [1] => b
    [2] => c
]

var_dump[]

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

echo "
";
var_dump[$arr];
echo "
";
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 "
";
var_export[$arr];
echo "
";
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

 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 
"; }
0 => a
1 => b
2 => c

In this post, we will see how to print an array in PHP.

There are several ways to print contents of an array in PHP:

1. Using print_r[] function

The most common method to print the contents of an array is using the print_r[] function, which displays the human information about a variable. For an array, the values are displayed in a format that shows keys and elements.

The following code demonstrates this by printing an associative array using the print_r[] function.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

 
Here’s how the output would look like for a normal array,

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

 
Note that you can capture the output of the print_r[] function by setting the second parameter to true. Now the print_r[] function will return the output rather than printing it.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

2. Using var_export[] function

The var_export[] function outputs or returns a parsable string representation of a variable, which is valid PHP code. It is often used by programmers to print an array in PHP.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

 
Here’s how the output would look like for a normal array,

3. Using var_dump[] function

Another good solution to dump information about the contents of an array is using the var_dump[] function. For an array, it recursively displays structured information about it such as its data type, value, and length.

The following code demonstrates this:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

 
Here’s how the output would look like for a normal array,

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

4. Using json_encode[] function

The json_encode[] function returns a string containing the JSON representation of a value. It can be used to encode an array in PHP, resulting in a human-readable JSON string.

Here’s how the output would look like for an associative array,

 
For a normal array, only the values are displayed.

5. Using foreach construct

Finally, you can use the foreach construct to iterate over arrays.

The following code demonstrates this, where it assigns the value of the current element to $value on each iteration:

 
The following code additionally assigns the current element’s key to the $key variable on each iteration.

 
Note: The whitespace is not preserved in the DOM, rather a single space is rendered in a browser. The solution is to wrap the output of the above functions inside a

 tag.

That’s all about printing an array in PHP.


Thanks for reading.

Please use our online compiler to post code in comments using C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.

Like us? Refer us to your friends and help us grow. Happy coding 🙂


How do you echo an array?

To echo an array, use the format echo ${Array[0]}. Array is your array name, and 0 is the index or the key if you are echoing an associative array. You can also use @ or * symbols instead of an index to print the entire array.

How do you display the value of an array?

To display array structure and values in PHP, we can use two functions. We can use var_dump[] or print_r[] to display the values of an array in human-readable format or to see the output value of the program array.

How we can print array in PHP?

There are several ways to print contents of an array in PHP:.
Using print_r[] function. ... .
Using var_export[] function. ... .
Using var_dump[] function. ... .
Using json_encode[] function. ... .
Using foreach construct..

How do I traverse an array in PHP?

There are several ways to traverse arrays in PHP, and the one you choose will depend on your data and the task you're performing..
The foreach Construct. ... .
The Iterator Functions. ... .
Using a for Loop. ... .
Calling a Function for Each Array Element. ... .
Reducing an Array. ... .
Searching for Values..

Chủ Đề