1

私が書いているのは、小さなボットネットで私のサイトを悩ませようとする人々のための一時的な禁止スクリプトです。

私が抱えている唯一の問題は、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 エンコードが配列としてではなくオブジェクトとしてエンコードすることです。問題は、以下が機能しないことです。

// 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 オブジェクトの設定を解除するにはどうすればよいですか?

4

2 に答える 2

6

json json_decode を使用するときは、ブール値の true を 2 番目のパラメーターとして渡します。

このようにして、血まみれの 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);

json オブジェクトの再帰的な変換を強制するために、json_decode の 2 番目のパラメーターとして常に true を渡します。

于 2012-08-03T07:40:19.317 に答える
3

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) }
于 2012-08-03T08:07:23.547 に答える