PHP - 如何将数字索引数组附加到数字索引的 StdClass 对象


PHP - How do you append a numerically indexed array to a numerically indexed StdClass Object?

对于我正在做的这个项目,我有几个约束:

  1. 纯 HTML/CSS/JavaScript(或 JS 插件(
  2. 可以使用 PHP 脚本,但不能用于页面/视图
  3. 无法使用数据库
  4. 保持简单

我正在尝试创建一种使用上述约束来跟踪错误或问题的方法。因此,我想出了使用JavaScript(带有jQuery(通过ajax将帖子数据发送到php脚本。然后,该脚本检查issues.json文件是否存在,如果不存在,则创建该文件。

如果它存在或已被创建,那么我使用 file_get_contents() 函数和 json_decode() 函数将其转换为 PHP 数组,或者在本例中为 StdClass Object

然后,我想将$_POST数据(在清理之后,即使这是一个内部工具(附加到 json 文件中找到的数据中。然后,我将使用所有新(和旧(数据重写json文件。是的,我几乎像使用贫民窟数据库一样使用它,但它符合我的约束!

杰伦示例

{
    "0": {
        "name": "John Doe",
        "email": "user@domain.com",
        "date": "2017-01-02",
        "issue_name": "Something",
        "issue_desc": "it's something for sure."
    }
}

PHP 我已经排除了卫生,检查和文件重写,因为我只是想先让这篇文章工作。

<?php
//Get the existing data.
$existing_data = file_get_contents("../json/issues.json");
$decoded = json_decode($existing_data);
//For Testing purposes
echo "<pre>";
    print_r($decoded);
echo "</pre>";
if(!empty($_POST)) {
    $_POST = (object) $_POST;
    $newData = array_merge((array)$decoded, array($_POST));
    //For Testing purposes
    echo "<pre>";
        print_r($newData);
    echo "</pre>";
}
$json = json_encode($newData);
echo $json;

此事务的结果如下:

Array
(
    [0] => stdClass Object
        (
            [name] => John Doe
            [email] => user@domain.com
            [date] => 2017-01-02
            [issue_name] => Something
            [issue_desc] => it's something for sure.
        )
    [0] => Array
        (
            [name] => John Doe
            [email] => john.doe@domain.net
            [date] => 2016-02-12
            [issue_title] => Problem with software
            [issue_desc] => the description of the problem
        )
)

我意识到将数组转换为对象的问题,反之亦然,如上所述。但是,我似乎无法让它像我想要的那样工作。

我试过使用 ArrayObject::append(( :

$arrayobj = new ArrayObject($decoded);
$arrayobj->append($_POST);

我得到一些奇怪的输出:

ArrayObject Object
(
    [storage:ArrayObject:private] => Array
        (
            [name] => John Doe
            [email] => john.doe@domain.net
            [date] => 2016-02-12
            [issue_title] => Problem with software
            [issue_desc] => the description of the problem.
            [0] => stdClass Object
                (
                    [0] => stdClass Object
                        (
                            [name] => John Doe
                            [email] => user@domain.com
                            [date] => 2017-01-02
                            [issue_name] => Something
                            [issue_desc] => it's something for sure.
                        )
                )
        )
)

我也尝试过array_merge(((请参阅代码(,但是如果没有对象/数组转换,它会抛出有关 StdClass 对象如何不是数组的错误(显然(。使用 array_merge() 函数的结果,就像我在代码中的结果一样,是(到目前为止(最接近我想要的结果。

期望的输出

stdClass Object
(
    [0] => stdClass Object
        (
            [name] => John Doe
            [email] => user@domain.com
            [date] => 2017-01-02
            [issue_name] => Something
            [issue_desc] => it's something for sure.
        )
    [1] => stdClass Object
        (
            [name] => John Doe
            [email] => john.doe@domain.net
            [date] => 2016-02-12
            [issue_title] => Problem with software
            [issue_desc] => the description of the problem
        )
)

我希望键递增,它会自然而然地这样做(如果我做得"正确"(,所以我可以添加任意数量的数据。

那么,现在回答我的问题(最后(:如何将数字索引数组(包含$_POST信息(附加到 PHP 中的数字索引 StdClass 对象?

我错过了什么吗?我已经在这里待了一整天,感觉我错过了一些基本的东西。任何帮助将不胜感激。谢谢你的时间!

已更新

检查我的代码,这按照要求工作:

$decoded = json_decode('{
    "0": {
        "name": "John Doe",
        "email": "user@domain.com",
        "date": "2017-01-02",
        "issue_name": "Something",
        "issue_desc": "it''s something for sure."
    }
}');
$_POST = array('name' => 'test');
$merged = (object)array_merge((array)$decoded, array((object)$_POST));
print_r($merged);

这给了我:

stdClass Object
(
    [0] => stdClass Object
        (
            [name] => John Doe
            [email] => user@domain.com
            [date] => 2017-01-02
            [issue_name] => Something
            [issue_desc] => it's something for sure.
        )
    [0] => stdClass Object
        (
            [name] => test
        )
)

我意识到所有的选角和取消选角都在使剧本膨胀。所以我做了更多的研究,问了一些同事,这就是我想出的:

//Get the existing data.
$existing_data = file_get_contents("../json/issues.json");
$decoded = json_decode($existing_data, true);
//Check if we're dealing with POST data
if(!empty($_POST)) {
    if(!empty($decoded)) {
        // Use array_unshift to put the newest issues at the beginning of the array, so they don't have to be sorted by date.
        array_unshift($decoded, $_POST);
    } else {
        $decoded = array();
        // Use array_unshift to put the newest issues at the beginning of the array, so they don't have to be sorted by date.
        array_unshift($decoded, $_POST);
    }
}
//Put the existing data.
$json = json_encode($decoded);
file_put_contents("../json/issues.json", $json);

解释首先,我获取 issues.json 文件的文件内容,并将其解码为 PHP 数组,将 $assoc 参数设置为 true,以便它根据 kunruh 的建议返回一个数组而不是一个对象。

然后我简单地检查我们是否从$_POST获取数据,如果是,我会检查我们是否从$decoded获取数据。因为如果 .json 文件为空,则在尝试将数组位推送到null时会导致内部服务器错误。因此,为了防止这种情况,我做了一个空数组,然后就推到了那个。

在该逻辑之后,重新编码它并将数据put回文件中是一件简单的事情。像魅力一样工作!希望这能够帮助其他人。