删除JSON文件的密钥

时间:2018-05-23 09:45:29

标签: php arrays json

我正在尝试从我的JSON文件中删除一个密钥,但我不知道“点击删除”如何知道它将删除它的密钥。 (PHP开始时用于添加“链接”并且它有效,问题是我不知道如何设置按钮“删除”以删除链接)我只想删除一个键例如{“name” :“Google”,“url”:“google.es”}并非所有阵列。 谢谢。 JSON:

[{"name":"Google","url":"google.es"},{"name":"Google","url":"google.es"},{"name":"gfdhd","url":"gfdhfg"},{"name":"gfdhd","url":"gfdhfg"}]

HTML / PHP

<?php
//ADD Links
if ($_SERVER['REQUEST_METHOD'] === 'POST') {

    var_dump($_POST);

    $jsonContents = file_get_contents('js/json.json');
    $name = $_POST['addname'];
    $url = $_POST['addlink'];
    $data = json_decode($jsonContents, true);
    $data[] = array(
        'name' => $name,
        'url' => $url
    );
    $json = json_encode($data);
    file_put_contents('js/json.json', $json);
}
?>
<?php
//RemoveLinks
if ($_SERVER['REQUEST_METHOD'] === 'POST') {

    var_dump($_POST);

    $jsonContents = file_get_contents('js/json.json');
    $ename = $_POST['edit-name'];
    $datae = json_decode($jsonContents, true);
    $datae[] = array(
        'name' => $ename,
        'url' => $url
    );
    $json = json_encode($datae);
    file_put_contents('js/json.json', $json);
}
?>
<!DOCTYPE html>
<html>

<head>
    <title>SSL Checker</title>
    <link rel="stylesheet" type="text/css" href="css/style.css">

    <script type="text/javascript" charset="utf-8">
        var Checker = <?php echo file_get_contents('js/json.json'); ?>;
    </script>
    <script type="text/javascript" src="js/script.js"></script>

</head>
<body onLoad="start()">
    <div id="title">
        <h1>SSL Checker</h1>
    </div>
    <div id="data">
        <form method="POST" onsubmit="SSL.Add()">
            <input type="text" name="addname" id="add-name" placeholder="Name" />
            <input type="text" name="addlink" id="add-link" placeholder="Link" />
            <input type="submit" value="Add" />
        </form>
        <div id="edit" role="aria-hidden">
            <form action="javascript:void(0);" method="POST" id="saveEdit">
                <input type="text" id="edit-name">
                <input type="submit" value="Edit" /> <a onclick="CloseInput()" aria-label="Close">&#10006;</a>
            </form>
        </div>
        <p id="counter"></p>
    </div>
    <div id="table">
        <table style="overflow-x:auto;">
            <tr>
                <th>Sites:</th>
            </tr>
            <tbody id="urls">
            </tbody>
        </table>
    </div>
</body>
</html>

JS:

function start() {
    var SSL = new function() {
        //List urls to check
        this.el = document.getElementById('urls');
        this.Count = function(data) {
            var el = document.getElementById('counter');
            var name = 'url';

            if (data) {
                if (data > 1) {
                    name = 'urls';
                }
                el.innerHTML = 'There are:' + ' ' + data + ' ' + name;
            } else {
                el.innerHTML = 'No ' + name;
            }
        };
        //Box/Table Configuration (Sites/edit/delete)
        this.FetchAll = function() {
            var data = '';

            if (Checker.length > 0) {
                for (i = 0; i < Checker.length; i++) {
                    data += '<tr>';
                    data += '<td><a href="http://' + Checker[i].url + '">' + Checker[i].name + '</a></td>';
                    data += '<td><button onclick="SSL.Edit(' + i + ')">Edit</button></td>';
                    data += '<td><button onclick="SSL.Delete(' + i + ')">Delete</button></td>';
                    data += '</tr>';

                }
            }

            this.Count(Checker.length);
            return this.el.innerHTML = data;
        };
        //Add name
        this.Add = function() {
            el = document.getElementById('add-name');
            el1 = document.getElementById('add-link')
            var url = el.value;
            var url1 = el1.value;
            if (url) {
                if (url) Checker.push({
                    "name": url,
                    "url": url1
                })

                this.FetchAll();
            }
        }

        //Edit
        this.Edit = function(item) {
            var el = document.getElementById('edit-name');
            var el1 = document.getElementById('edit-name1');
            el.value = Checker[item].name;
            el1.value = Checker[item].url;
            document.getElementById('edit').style.display = 'block';
            self = this;
            document.getElementById('saveEdit').onsubmit = function() {
                var url = el.value;
                var url1 = el1.value;
                if (url) {
                    Checker[item].url = url1.trim();
                    Checker[item].name = url.trim();
                    self.FetchAll();
                    CloseInput();
                }
            }
        };
        //Delete
        this.Delete = function(item) {
            Checker.splice(item, 1);
            this.FetchAll();
        };

    };

    SSL.FetchAll();
    //Close button (Edit bar)
    function CloseInput() {
        document.getElementById('edit').style.display = 'none';
    }
    window.CloseInput = CloseInput;
    window.SSL = SSL;
}

1 个答案:

答案 0 :(得分:2)

首先在解码后使用json解码您的unset值以删除特定密钥,例如

    $json_val='[{"name":"Google","url":"google.es"},
    {"name":"Google","url":"google.es"},{"name":"gfdhd","url":"gfdhfg"},
    {"name":"gfdhd","url":"gfdhfg"}]';
    $data=json_decode($json_val);
    echo "<pre>";
    print_r($data);
    unset($data[0]);
    print_r($data);

输出:

Array
(
    [0] => stdClass Object
        (
            [name] => Google
            [url] => google.es
        )

    [1] => stdClass Object
        (
            [name] => Google
            [url] => google.es
        )

    [2] => stdClass Object
        (
            [name] => gfdhd
            [url] => gfdhfg
        )

    [3] => stdClass Object
        (
            [name] => gfdhd
            [url] => gfdhfg
        )

)
Array
(
    [1] => stdClass Object
        (
            [name] => Google
            [url] => google.es
        )

    [2] => stdClass Object
        (
            [name] => gfdhd
            [url] => gfdhfg
        )

    [3] => stdClass Object
        (
            [name] => gfdhd
            [url] => gfdhfg
        )

)