How to remove an object from an array of objects in a foreach php loop

I have a users object returned from a database that looks like this:

 [ { "id":1, "name":"Bob", "category":"admin", "email":" test1@test.com ", "phone":"123456789", "gender":"male", }, { "id":2, "name":"John", "category":"user", "email":" john@test.com ", "phone":"123456789", "gender":"male", }, { "id":3, "name":"Jane", "category":"admin", "email":" jane@test.com ", "phone":"123456789", "gender":"female", }, ] 

Now I want to go through the entire users object and remove all users whose category user attribute, so that the resulting users object remains only with admin .

I want to do this with a foreach loop. please, what should I do about this or what is the best way I can do this?

+6
source share
4 answers

I assume the array was in JSON format? Anyway .. I fixed your invalid JSON so you can see how this is done.

 $json = ' [{ "id": 1, "name": "Bob", "category": "admin", "email": " test1@test.com ", "phone": "123456789", "gender": "male" }, { "id": 2, "name": "John", "category": "user", "email": " john@test.com ", "phone": "123456789", "gender": "male" }, { "id": 3, "name": "Jane", "category": "admin", "email": " jane@test.com ", "phone": "123456789", "gender": "female" }] '; $array = json_decode($json, true); //Fixed and converted JSON into PHP Assoc Array foreach($array as $k=>$v) { foreach ($array[$k] as $key=>$value) { if ($key === "category" && $value === "user") { //If Value of 2D is equal to user and cat unset($array[$k]); //Delete from Array } } } var_dump($array); //Output Array 

Exit

 array(2) { [0] => array(6) { ["id"] => int(1)["name"] => string(3) "Bob" ["category"] => string(5) "admin" ["email"] => string(14) " test1@test.com " ["phone"] => string(9) "123456789" ["gender"] => string(4) "male" }[2] => array(6) { ["id"] => int(3)["name"] => string(4) "Jane" ["category"] => string(5) "admin" ["email"] => string(13) " jane@test.com " ["phone"] => string(9) "123456789" ["gender"] => string(6) "female" } } 

Edit

* As @sevavietl noted in the comments, if any other element of the array was called by the user, this will be deleted. I changed the code to check the key (category) as well as the value. *

+3
source

Assuming the array is in json format, one line code:

 $users = array_filter(json_decode($users), function($user) { return $user->category != 'user'; }); 

If this is already a valid php array, just remove json_decode()

+5
source

Your input looks like JSON , so you can use the json_decode function to make it an array that you can foreach , and you can encode ( json_encode ) back to JSON if you want the same output.


Decision

The code below should be useful for you:

 <?php // $json = output from your db $json_output = json_decode($json, true); foreach ($json_output as $user) { if ($user['category'] == 'admin') { // or it may be $user['category'] != 'user' $user_output[] = $user; } } print_r($user_output); print_r(json_encode($user_output, true)); // if you want json output ?> 

SQL

You can also add a WHERE query to your SQL query. It might look like this:

 SELECT * FROM users WHERE category = 'admin' 

Of course, the query depends on the names that you have in your table. You can read more about the SELECT query in the manuals below.


Guides

More details on the methods used, which you can read below:


Online compiler

You can check how it works here.

+1
source

If you just want to delete the category "user", you just need to get the key and make a condition to delete this category and put the category "admin" in this new array. Look at this:

 $json = ' [{ "id": 1, "name": "Bob", "category": "admin", "email": " test1@test.com ", "phone": "123456789", "gender": "male" }, { "id": 2, "name": "John", "category": "user", "email": " john@test.com ", "phone": "123456789", "gender": "male" }, { "id": 3, "name": "Jane", "category": "admin", "email": " jane@test.com ", "phone": "123456789", "gender": "female" }]'; $arr = json_decode($json, true); $newArr = array(); foreach($arr as $key => $value) { if($value["category"] == "admin") { $newArr[] = $value; } } echo '<pre>'; print_r($newArr); echo '</pre>'; 
+1
source

Source: https://habr.com/ru/post/1012726/


All Articles