Как добавить значения в массив PHP - PullRequest
0 голосов
/ 22 мая 2018

Я пытаюсь добавить значения в файл, но это мой первый раз с PHP, и я не могу найти способ, как я могу это сделать?

Цель состоит в том, чтобы добавить значения из формуляра (add-name и add-link ID) в файл JSON с той же структурой и сохраните его.(Не временно, чтобы сохранить его в файл).Я прошу ввести ключ, например: {"name": "google", "url": "google.es}

<!DOCTYPE html>
<html>

<head>

    <title>SSL Checker</title>
    <link rel="stylesheet" type="text/css" href="css/style.css">
    <script type="text/javascript" src="js/script.js"></script>
    <script type="text/javascript" src="js/json.json" charset="utf-8"></script>
</head>

<body onLoad="start()">
    <div id="title">
        <h1>SSL Checker</h1>
    </div>
    <div id="data">
        <form action="javascript:void(0);" method="POST" onsubmit="SSL.Add()">
            <input type="text" id="add-name" placeholder="Name"></input>
            <input type="text" id="add-link" placeholder="Link"></input>
            <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><br>
                <input type="text" id="edit-name1">
            </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>

JSON:

var Checker = [{
        name:"Google",
        url: "google.es",
    },
    {
        name:"Yahoo",
        url: "yahoo.com",
    }
]

1 Ответ

0 голосов
/ 22 мая 2018
<?php

$checkers = []; // assign an empty array to the variable $checkers;

// cast associative array to an object and add to the checker array
$checkers[] = (object)['name' => 'Google', 'url' => 'google.es'];

// create an object using the predefined stdClass 
$item = new stdClass();
$item->name = 'Yahoo';
$item->url = 'yahoo.com';

$checkers[] = $item;

// ideally create your own class representing the checker object

echo json_encode($checkers);

Выходы:

[{
    "name": "Google",
    "url": "google.es"
}, {
    "name": "Yahoo",
    "url": "yahoo.com"
}]

Для получения дополнительной информации перейдите по этой ссылке: Как в PHP добавить элемент объекта в массив?

...