我从数据库返回的users
对象如下所示:
[
{
"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",
},
]
现在,我想遍历所有users
对象并删除category
属性为user
的所有用户,以便生成的users
对象仅保留{{1类别。
我想用foreach循环来做这件事。请问我该如何解决这个问题?或者我有什么更好的方法来实现这个目标?
答案 0 :(得分:5)
假设数组是json格式,一行代码:
$users = array_filter(json_decode($users), function($user) {
return $user->category != 'user';
});
如果它已经是一个有效的php数组,只需删除json_decode()
答案 1 :(得分:3)
我猜这个数组是JSON格式的吗?无论如何..我修复了你的无效JSON,所以你应该能够看到它是如何完成的。
$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
<强>输出强>
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"
}
}
修改的
*正如@sevavietl在评论中指出的那样,如果数组的任何其他元素被称为user,那么这将被删除。我已经将代码更改为现在测试密钥(类别)以及值。 *
答案 2 :(得分:1)
您的输入看起来像JSON
,因此您可以使用json_decode
函数将其设为数组,您可以foreach
,并且可以对其进行编码(json_encode
)如果你想要相同的输出,请回到JSON
。
<强>解决方案强>
以下代码应该对您有用:
<?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 强>
您还可以添加SQL查询WHERE
语句。它可能如下所示:
SELECT * FROM users WHERE category = 'admin'
当然查询取决于您在表中的名称。有关SELECT
查询的更多信息,请参阅以下手册。
<强>手册强>
有关您可以阅读的使用方法的更多信息:
在线编译
您可以查看其工作原理here
答案 3 :(得分:1)
如果您只想删除“user”类别,则只需获取密钥并为删除该类别创建条件,并将类别“admin”放在该新阵列上。看看这个:
$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>';