I'm using array_filter
to search and remove unwanted results I'm not good English, here's how I am doing it:
$obj_Data = [
[
"id" => 1,
"level" => "admin",
"name" => "jack"
],
[
"id" => 2,
"level" => "member",
"name" => "john"
],
[
"id" => 3,
"level" => "member",
"name" => "jenny"
],
[
"id" => 4,
"level" => "member",
"name" => "whatever"
]
];
function filter_callback($element) {
if ($element["level"] == "member") {
return TRUE;
}
return FALSE;
}
$arr["data"] = array_filter($obj_Data, "filter_callback");
echo json_encode($arr);
it gives me result like:
{
"data": {
"1": {
"id": 2,
"level": "member",
"name": "john"
},
"3": {
"id": 4,
"level": "member",
"name": "whatever"
}
}
}
This is the result I want:
{
"data": {
{
"id": 2,
"level": "member",
"name": "john"
},
{
"id": 4,
"level": "member",
"name": "whatever"
}
}
}
How can I remove "1" and "3"? Cause I can't use loop for it, thanks for reading.
array_filter()
retains the used array keys, making the resulting array an object in the context of JSON. Simply re-index the array through array_values()
before JSON-encoding it.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.