Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why using unset() function cast the array to object?

Here is my code:

return ApiResponse::Json(200, '', ['categories' => $categories], 200);

And here is a screenshot of the result:

output 1

Now, I need to unset items inside categories collection based on a specific logic. So I wrote this loop:

foreach ($categories as $key => $category) {
    if ($category->BusinessSubCategory->isEmpty())
          unset($categories[0]);
}

return ApiResponse::Json(200, '', ['categories' => $categories], 200);

And here is my new result: (which is casted to an object)

output 2

Well .. What's wrong? How can I keep the old structure after unseting some items?

like image 310
Mohammad Gholamrezaei Avatar asked Jul 14 '21 11:07

Mohammad Gholamrezaei


People also ask

What does the unset () function mean?

Definition and Usage The function unset() destroys the specified variables. The behavior of unset() inside of a function can vary depending on what type of variable you are attempting to destroy. If a globalized variable is unset() inside of a function, only the local variable is destroyed.

What happens when you use unset () to remove a value from an array?

The unset function is used to destroy any other variable and same way use to delete any element of an array. This unset command takes the array key as input and removed that element from the array. After removal the associated key and value does not change.

What is the use of isset () and unset ()?

PHP isset() Function This function returns true if the variable exists and is not NULL, otherwise it returns false. Note: If multiple variables are supplied, then this function will return true only if all of the variables are set. Tip: A variable can be unset with the unset() function.

Which function is used to unset a variable?

The unset() function is a predefined variable handling function of PHP, which is used to unset a specified variable. In other words, "the unset() function destroys the variables".

How to make the UN-setting array functional in PHP?

To make the un-setting array functional we need to have an array with some value on that. Let’s say, we have an array named $array1 with some value. Now we need to make this array empty, we can do this by using the PHP unset () function. We can also delete an array element by using the PHP unset feature.

How to delete an array element in PHP?

We can also delete an array element by using the PHP unset feature. We can do the below mentioned in the PHP unset array functionalities: Unset an array. Unset an element of an array with its index. Unset array by its value – this can’t be directly achieved.

How to convert an object to an array in JavaScript?

Also read, how to convert an Object to an Array in Javascript. 2. Using Spread Operator In this above code snippet, we have used the reduce function and along with that, we have used spread operator (…) which will basically spread the previous object for every iteration and add new values to it.


Video Answer


2 Answers

Try this:

foreach ($categories as $key => $category) {
    if ($category->BusinessSubCategory->isEmpty())
          unset($categories[$key]);

}

$categories = array_slice($categories->toArray(), 0, count($categories));

return ApiResponse::Json(200, '', ['categories' => $categories], 200);
like image 165
Shafizadeh Avatar answered Oct 27 '22 14:10

Shafizadeh


The unset() method doesn't change your type.

Your 'array' is not an array but a collection.

Try:

foreach ($categories as $key => $category) {
    if ($category->BusinessSubCategory->isEmpty()) {
        unset($categories[0]);
        break;
    }
}
$categories = array_values($categories->toArray());

return ApiResponse::Json(200, '', ['categories' => $categories], 200);

Notice if you're unseting the same element you should break loop after do that.

like image 1
LordF Avatar answered Oct 27 '22 14:10

LordF