【问题标题】:PHP : Work with stdClass() for create a jsonPHP:使用 stdClass() 创建 json
【发布时间】:2014-12-16 10:06:34
【问题描述】:

我的目标是用 php 读取一个复杂的 sqlite 数据库,并创建一个 json 代表该数据库按照一定的规则 p>

为此,我决定使用 stdClass()

问题:当我调用 TEST() 函数时,block3 没有被添加

测试代码(我尽量代表情况,当然这里我不使用数据库和foreach)

$OBJ = new stdClass();

$OBJ->block1 = array();

$OBJ->block1[0]["prop1"] = "test";
$OBJ->block1[0]["prop2"] = "test";
$OBJ->block1[0]["prop3"] = "test";
$OBJ->block1[0]["prop4"] = "test";



$OBJ->block2 = array();
for($i=0;$i<3;$i++) {
    $OBJ->block2[$i]["sxsxs"] = "test";
    $OBJ->block2[$i]["98u98u"] = "test";
    $OBJ->block2[$i]["jhjh"] = "test";
    $OBJ->block2[$i]["oiuoiu"] = "test";

    //this WORKS , but i want to do this in the TEST function
    //$OBJ->block2[$i]["block3"] = array();
    TEST($OBJ->block2[$i]);

}


function TEST($c){
    $c["block3"] = array();
}

echo json_encode($OBJ);

这是我想要的,一切都是正确的,但不是block3的添加

{
    "block1": [
        {
            "prop1": "test",
            "prop2": "test",
            "prop3": "test",
            "prop4": "test"
        }
    ],
    "block2": [
        {
            "sxsxs": "test",
            "98u98u": "test",
            "jhjh": "test",
            "oiuoiu": "test"
            "block3" : []
        },
        {
            "sxsxs": "test",
            "98u98u": "test",
            "jhjh": "test",
            "oiuoiu": "test"
            "block3" : []
        },
        {
            "sxsxs": "test",
            "98u98u": "test",
            "jhjh": "test",
            "oiuoiu": "test"
            "block3" : []
        }
    ]
}

我无法使用对类 TEST 的调用,但我想划分各个进程


【问题讨论】:

  • 那是因为$OBJ-&gt;block2[$i] 不是一个对象,而是一个数组。
  • 如果我试过这个 $OBJ->block2[0]["block3"] = array(); ,刚出for,块添加正确
  • $OBJ->block1 是一个数组。 $OBJ->block1[$i] 不是数组
  • 我更新了问题,有什么建议吗?谢谢

标签: php json stdclass


【解决方案1】:

您发送到 TEST 函数的变量不是对象。这是一个数组。

在 TEST 函数中对 $c 做一个 var_dump,你会得到:

数组(4){ ["sxsxs"]=> 字符串(4)“测试” [“98u98u”]=> 字符串(4)“测试” [“jhjh”]=> 字符串(4)“测试” ["oiuoiu"]=> 字符串(4)“测试” }

您不能使用 -> 运算符将变量添加到数组中。 也许这就是你试图完成的:

$OBJ = new stdClass();

$OBJ->block1 = array();

$OBJ->block1[0]["prop1"] = "test";
$OBJ->block1[0]["prop2"] = "test";
$OBJ->block1[0]["prop3"] = "test";
$OBJ->block1[0]["prop4"] = "test";



$OBJ->block2 = array();
for($i=0;$i<3;$i++) {
    $OBJ->block2[$i]["sxsxs"] = "test";
    $OBJ->block2[$i]["98u98u"] = "test";
    $OBJ->block2[$i]["jhjh"] = "test";
    $OBJ->block2[$i]["oiuoiu"] = "test";

    TEST($OBJ);

}


function TEST($c){
    $c->block3 = array();
}


echo json_encode($OBJ);

输出:

{"block1":[{"prop1":"test","prop2":"test","prop3":"test","prop4":"test"}],"block2":[{ "sxsxs":"test","98u98u":"test","jhjh":"test","oiuoiu":"test"},{"sxsxs":"test","98u98u":"test", "jhjh":"test","oiuoiu":"test"},{"sxsxs":"test","98u98u":"test","jhjh":"test","oiuoiu":"test"} ],"block3":[]}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-12-18
    • 2023-03-07
    • 2016-10-27
    • 1970-01-01
    • 2021-12-01
    • 1970-01-01
    • 2014-02-16
    • 2018-09-05
    相关资源
    最近更新 更多