Here is my code:
return ApiResponse::Json(200, '', ['categories' => $categories], 200);
And here is a screenshot of the result:
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)
Well .. What's wrong? How can I keep the old structure after unseting some items?
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.
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.
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.
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".
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.
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.
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.
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);
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With