PHP array_unique returning object on json_encode [Solved]

February 13, 2023
PHP
PHP array_unique returning object on json_encode [Solved]

In this guide, you will learn why PHP array_unique function is giving the object instead of array when you do json_encode and how to solve that problem.

Let’s get understand!

What does array_unique do in PHP?

To understand our problem, we first need to understand what does array_unqiue function in PHP. It simply remove the duplicates from the values where it array or the object itself without repositioning or changing its keys.

1- Example of array_unqiue if object given:

If you give the following object to the array_unqiue

$input = array("a" => "green", "red", "b" => "green", "blue", "red");
$unique = array_unique($input);
print_r($unique);

It will give the following result:

Array
(
    [a] => green
    [0] => red
    [1] => blue
)

By respecting the position of key, it will only check for the values which are the same and removes them.

2- Example of array_unqiue if array given:

If you give the following array to array_unique function

$input = array("green", "red", "blue","green","red",'orange');
$unique = array_unique($input);
print_r($unique);

It will give the following result:

Array
(
    [0] => green
    [1] => red
    [2] => blue
    [5] => orange
)

Since we have the array itself, it will remove the duplicates of values, keeping the position of key.

Why does array_unique returning object problem on json_encode?

The reason for that is the array_unique only remove the duplicate values and do not reset or change the positions of the keys. And since there is gap of keys that’s why json_encode showing you as the object. Instead of json_encode if you type print_r , you will see this

$unique = array_unique($input);
$renumbered = (array_unique($unique));
print_r($renumbered);

//result
Array
(
    [0] => green
    [1] => red
    [2] => blue
    [5] => orange
)

How to solve array_unique returning object problem on json_encode?

From above explanation you should by now understand why we are getting an object instead of array.

So to solve array_unique returning object on json_encode is that you just need to apply another PHP function which is array_values, here is how you do it:

$input = array("green", "red", "blue","green","red",'orange');
$unique = array_unique($input);
$renumbered = array_values(array_unique($unique));

echo json_encode($renumbered);

//result
["green","red","blue","orange"]

So in the above code, array_values function only get the values of the array where its the object or array itself, which solves our problem and give us the array as the result. Here is the what the result will look like if you do print_r:

$input = array("green", "red", "blue","green","red",'orange');
$unique = array_unique($input);
$renumbered = array_values(array_unique($unique));

print_r($renumbered);


//result
Array
(
    [0] => green
    [1] => red
    [2] => blue
    [3] => orange
)

Conclusion

Hope you have learned today, how to understand solve the PHP array_unique returning object problem on json_encode .

See you in the next one!

Tags

Write a Reply or Comment

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


Icon