If an array is printed as ‘Array’, how do you solve it?

TheDevStory
2 min readNov 10, 2022

--

php
php

Suppose you want to print an array on the screen. So we wrote code like below.

<?php
(print_r (
"<pre>".
get_function($user_id, "some array data")
."</pre>"
));
?>

This is a simple code. And of course we expect the array to be printed well.

What?

That is not what we want to see. But there is no need to panic. Array was printed and that mean the function worked well. The problem is the format of the data we received. The reason for printing ‘array’ is mostly due to data formatting issues.It means that the data we receive is raw data. Encoding is required.

In this situation, we can use json.encode() .

print_r (
"<pre>".
json_encode(get_function($user_id, "some array data"))
."</pre>"
)

And let’s check that.

Now we can print well.

Remember, if the information type is printed as it is, most of them can be solved by just encoding.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

TheDevStory
TheDevStory

Written by TheDevStory

A web developer crafting online experiences. Also football(soccer) coach and Spanish Learner.

No responses yet

Write a response