在 PHP 中将新的键/值对添加到 JSON 中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19447175/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 19:29:17  来源:igfitidea点击:

Add new key/value pair into JSON in PHP

phpjson

提问by OussamaLord

I have a result from my MySQL DB that I'm json encoding in PHP, the result looks like :

我的 MySQL 数据库有一个结果,我在 PHP 中进行了 json 编码,结果如下:

[
    {
        "id": "8488",
        "name": "Tenby",
        "area": "Area1"
    },
    {
        "id": "8489",
        "name": "Harbour",
        "area": "Area1"
    },
    {
        "id": "8490",
        "name": "Mobius",
        "area": "Area1"
    }
] 

What I would like to do is to add a new key/value pair to that JSON so that it will be :

我想要做的是向该 JSON 添加一个新的键/值对,以便它是:

[
    {
        "id": "8488",
        "name": "Tenby",
        "area": "Area1",
        "image": "1278.jpg"
    },
    {
        "id": "8489",
        "name": "Harbour",
        "area": "Area1",
        "image": "1279.jpg"
    },
    {
        "id": "8490",
        "name": "Mobius",
        "area": "Area1",
        "image": "1280.jpg"
    }
]

So how can I do that in PHP?

那么我怎样才能在 PHP 中做到这一点呢?

回答by Jenson M John

<?php

$data[0]['id']="8488";
$data[0]['name']="Tenby";
$data[0]['area']="Area1";

$data[1]['id']="8489";
$data[1]['name']="Harbour";
$data[1]['area']="Area1";

$data[2]['id']="8490";
$data[2]['name']="Mobius";
$data[2]['area']="Area1";

echo json_encode($data)."<br/>";

/*Add Image element (or whatever) into the array according to your needs*/

$data[0]['image']="1278.jpg";
$data[1]['image']="1279.jpg";
$data[2]['image']="1280.jpg";

echo json_encode($data);

?>

回答by cypherabe

hu?

呼?

the result of the db query will be an array or an object... add the additional entry to that data, only encode after every necessary data manipulation is done

db 查询的结果将是一个数组或一个对象...向该数据添加附加条目,仅在完成所有必要的数据操作后进行编码

alternatives, but clumsy (horrible):

替代方案,但笨拙(可怕):

  • json_decode, add stuff, json_encode
  • build your additional data as a string, str_replacefor example "area": "Area1"in your json string with "area": "Area1", "image": "1278.jpg"
  • json_decode, 添加东西, json_encode
  • 将您的附加数据构建为字符串,str_replace例如"area": "Area1"在您的 json 字符串中使用"area": "Area1", "image": "1278.jpg"

but really: output formatting like json_encode should only be done once you are sure that you have the whole output together and it is send out

但实际上:像 json_encode 这样的输出格式应该只在您确定将整个输出放在一起并发送出去时才进行

回答by Sahil Sakhuja

PHP is not very good with JSON. Its best to convert from JSON to Array to do this - what @egig has also recommended.

PHP 对 JSON 不是很好。最好将 JSON 转换为 Array 以执行此操作 - @egig 也建议这样做。

Example code:

示例代码:

$temp = json_decode($json);
$temp[] = new data, whatever you want to add...;
$json = json_encode($temp);

Hope this helps.

希望这可以帮助。

回答by JamLizzy101

Maybe things have changed since then, but I know this will work at this current stage:-

也许从那以后事情发生了变化,但我知道这将在当前阶段起作用:-

So here is the JSON string:-

所以这里是 JSON 字符串:-

$strJSON = '[
  {
      "id": "8488",
      "name": "Tenby",
      "area": "Area1"
  },
  {
      "id": "8489",
      "name": "Harbour",
      "area": "Area1"
  },
  {
      "id": "8490",
      "name": "Mobius",
      "area": "Area1"
  }
]';

If this is still a string, then I would convert it into an object like this:-

如果这仍然是一个字符串,那么我会将其转换为这样的对象:-

$json = json_decode( $strJSON );

Now that it is in object format, to add my "image" keys with their values, I would then add them like it is shown below:-

现在它是对象格式,要添加我的“图像”键及其值,然后我将添加它们,如下所示:-

$json[0]->image = "1278.jpg";
$json[1]->image = "1279.jpg";
$json[2]->image = "1280.jpg";

So then if I add the code below after the above code:-

那么如果我在上面的代码之后添加下面的代码:-

echo "<pre>";
print_r( $json );
echo "</pre>";

We should then see it outputting on the page like so:-

然后我们应该看到它在页面上输出,如下所示:-

Array
(
    [0] => stdClass Object
        (
            [id] => 8488
            [name] => Tenby
            [area] => Area1
            [image] => 1278.jpg
        )

    [1] => stdClass Object
        (
            [id] => 8489
            [name] => Harbour
            [area] => Area1
            [image] => 1279.jpg
        )

    [2] => stdClass Object
        (
            [id] => 8490
            [name] => Mobius
            [area] => Area1
            [image] => 1280.jpg
        )

)

回答by Inamur Rahman

//$index = some value in array;

//$index = 数组中的某个值;

for($i=0;$i<count($index);$i++){
            $data[$i]->key = $index[$i];
        }
print $data;