【问题标题】:How to unset a json object如何取消设置 json 对象
【发布时间】:2012-08-01 05:40:35
【问题描述】:

我正在写的是一个暂时禁止那些喜欢用小型僵尸网络纠缠我的网站的人的脚本。

我遇到的唯一问题是如何取消设置 json 对象。

我有以下代码

/* JSON blocking script written by Michael Dibbets
 * Copyright 2012 by Michael Dibbets
 * http://www.facebook.com/michael.dibbets - mdibbets[at]outlook.com
 * Licenced under the MIT license http://opensource.org/licenses/MIT
 */
    // Turn on error reporting
    ini_set("display_errors", 1);
    error_reporting(E_ALL);  
    // Create our codeigniter filepath
    $filepath = FCPATH.'pagemodules/blocked.json';
    // Load file helper to be able to be lazy
    $this->load->helper('file');
    // Read the json file
    $data = read_file($filepath,'c+');
    // Have we succeeded? Then continue, otherwise fail graciously
    if($data !== false)
        {
            // Let's make it readable
        $json = json_decode($data);
            // Display it for debug purposes
        echo $data;
            // Iterate through every object, get the key to be able to unset it
        foreach($json as $key => $obj)
            {
                    // Dump the object for debug purposes
            var_dump($obj);
            echo "<P>";
                    // Has it's life time expired?
            if((int)$obj->{'endtime'} < strtotime("+2 hours 2 minutes"));
                {
                            // remove the object from the array
                unset($json[$key]);
                }
            }
            // Remove the file so we can overwrite it properly
        unlink($filepath); 
        }
    // Add some values to our array
    $json[] = array('ip' => $_SERVER['REMOTE_ADDR'],'endtime' => strtotime('+2 hours'));
    // Encode it
    $data = json_encode($json);
    // Write it to file
    write_file($filepath,$data,'c+'); 

我遇到的问题是 json encode 不会将其编码为数组而是作为对象。问题是以下不起作用:

// This gives the error Fatal error: Cannot use object of type stdClass as array in /public_html/ocs_application/controllers/quick.php on line 37
unset($json[$key]);
// This doesn't report anything, and does nothing
unset($json->{$key});

如何取消设置 json 对象?

【问题讨论】:

标签: php json codeigniter unset


【解决方案1】:

老兄,当您使用 json 时,json_decode 将布尔值 true 作为第二个参数传递给它。

这样你就可以驾驭该死的stdClass了。

现在如果你想删除一个 json 对象,基本上它是一个字符串,所以只需做一些链接 $var = null;

如果您想取消设置其中的一部分,则必须先对其进行解码,然后再对其进行编码。

$my_var = json_decode($json, true); // convert it to an array.

unset($my_var["key_of_value_to_delete"]);

$json = json_encode($my_var);

始终将 true 作为第二个参数传递给 json_decode,以强制它对 json 对象进行递归转换。

【讨论】:

  • 老兄,你真是天赐良机!非常感谢。这让我的生活轻松了很多!以前应该注意到它,但陷入了困境。非常感谢!你给我上了关于 json 解码的宝贵一课。
  • 为什么取消更改对象的 json 数组?我的 json 是: [{"proId":"3","sizeId":"1","number":1},{"proId":"1","sizeId":"","number":" 10"},{"proId":"3","sizeId":"1","number":1}] 但像这样取消设置后:{"0":{"proId":"3","colorId ":"1","sizeId":"1","number":1},"2":{"proId":"3","colorId":"2","sizeId":"1", “数字”:1}}
  • @farzad 我有同样的问题,编码不能正常工作,它在 json 上添加了一些额外的数字。 Array ( [0] => Array ( [platform] => x [link] => x [videono] => x ) [1] => Array ( [platform] => eders1 [link] => 313389 [videono] => 0 ) ) 这个数组返回这个 json {"0":{"platform":"x","link":"x","videono":"x"},"1":{"platform" :"eders1","link":"313389","videono":"0"}}
【解决方案2】:

只是为了明确unset() 可用于 JSON 对象和数组:

$json_str = '{"foo": 1, "bar": 2, "baz": 3}';
$json_obj = json_decode($json_str);
$json_arr = json_decode($json_str, true);

var_dump($json_obj); // object(stdClass)#1 (3) { ["foo"]=> int(1) ["bar"]=> int(2) ["baz"]=> int(3) }
var_dump($json_arr); // array(3) { ["foo"]=> int(1) ["bar"]=> int(2) ["baz"]=> int(3) }    

unset($json_obj->baz);
unset($json_arr['baz']);

var_dump($json_obj); // object(stdClass)#1 (2) { ["foo"]=> int(1) ["bar"]=> int(2) }
var_dump($json_arr); // array(2) { ["foo"]=> int(1) ["bar"]=> int(2) }

$key = 'bar';
unset($json_obj->$key);
unset($json_arr[$key]);

var_dump($json_obj); // object(stdClass)#1 (1) { ["foo"]=> int(1) }
var_dump($json_arr); // array(1) { ["foo"]=> int(1) }

【讨论】:

  • 好的,如果您不知道要取消设置的对象名称怎么办?就像在我的示例中,如果您在 json 解码中不使用 true 变量?
猜你喜欢
  • 2012-11-07
  • 1970-01-01
  • 2018-02-23
  • 2015-09-11
  • 1970-01-01
  • 1970-01-01
  • 2011-02-05
  • 2016-12-14
  • 2020-12-15
相关资源
最近更新 更多