Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to remove object from array of objects in foreach loop php

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

[
    {
        "id":1,
        "name":"Bob",
        "category":"admin",
        "email":"[email protected]",
        "phone":"123456789",
        "gender":"male",
    },
    {
        "id":2,
        "name":"John",
        "category":"user",
        "email":"[email protected]",
        "phone":"123456789",
        "gender":"male",
    },
    {
        "id":3,
        "name":"Jane",
        "category":"admin",
        "email":"[email protected]",
        "phone":"123456789",
        "gender":"female",
    },
]

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

I want to do this with a foreach loop. please how do I go about this or what better ways can I accomplish this?

like image 415
Awa Melvine Avatar asked Nov 29 '16 12:11

Awa Melvine


People also ask

How do I remove an item from an array of objects?

Use array. forEach() method to traverse every object of the array. For each object use delete obj. property to delete the certain object element from array of objects.

How can I remove a specific item from an array PHP?

In order to remove an element from an array, we can use unset() function which removes the element from an array and then use array_values() function which indexes the array numerically automatically.

Can you modify array in foreach?

Note that foreach does not modify the internal array pointer, which is used by functions such as current() and key(). It is possible to customize object iteration. In order to be able to directly modify array elements within the loop precede $value with &. In that case the value will be assigned by reference.

Can I use foreach on Object in PHP?

PHP provides a way for objects to be defined so it is possible to iterate through a list of items, with, for example a foreach statement. By default, all visible properties will be used for the iteration.


2 Answers

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

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

If it is instead already a valid php array simply remove the json_decode()

like image 178
maestroosram Avatar answered Nov 14 '22 22:11

maestroosram


I'm guessing the array was in JSON format? Anyways.. I fixed your invalid JSON so you should be able to see how it's done.

$json = '

[{
    "id": 1,
    "name": "Bob",
    "category": "admin",
    "email": "[email protected]",
    "phone": "123456789",
    "gender": "male"
}, {
    "id": 2,
    "name": "John",
    "category": "user",
    "email": "[email protected]",
    "phone": "123456789",
    "gender": "male"
}, {
    "id": 3,
    "name": "Jane",
    "category": "admin",
    "email": "[email protected]",
    "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

Output

array(2) {
    [0] => array(6) {
        ["id"] => int(1)["name"] => string(3)
        "Bob" ["category"] => string(5)
        "admin" ["email"] => string(14)
        "[email protected]" ["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)
        "[email protected]" ["phone"] => string(9)
        "123456789" ["gender"] => string(6)
        "female"
    }
}

Edit

*As @sevavietl pointed out in the comments, if any other element of the array was called user, then this would be removed. I've changed code to now test the key (category) as well as the value. *

like image 38
Kitson88 Avatar answered Nov 14 '22 23:11

Kitson88