使用php从json删除 [英] Delete from json using php

查看:188
本文介绍了使用php从json删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当前的json代码:

my Current json code :

{"Results":[{"username":"test","password":"test"},{"username":"test","password":"test"},{"username":"google","password":"test"},{"username":"yahoo","password":"test"},{"username":"hotmail","password":"test"}]}

我要删除此内容:

{"username":"google","password":"test"}

从使用php的代码开始. 我尝试通过将json解码为数组来删除,但无法完成. 有什么解决办法吗?

from the code using php. i tried deleting by decoding json to array but cant get it done. any solution ?

推荐答案

$json_obj = json_decode($json_string);
$unset_queue = array();

foreach ( $json_obj->Results as $i => $item )
{
    if ($item->username == "google")
    {
        $unset_queue[] = $i;
    }
}

foreach ( $unset_queue as $index )
{
    unset($json_obj->Results[$index]);
}

// rebase the array
$json_obj->Results = array_values($json_obj->Results);

$new_json_string = json_encode($json_obj);

这篇关于使用php从json删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆