簡體   English   中英

在PHP中將新的鍵/值對添加到JSON中

[英]Add new key/value pair into JSON in PHP

我的MySQL數據庫的結果是我在PHP中使用json編碼,結果如下:

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

我想要做的是為該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"
    }
]

那我怎么能在PHP中做到這一點?

<?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);

?>

胡?

db查詢的結果將是一個數組或一個對象...向該數據添加附加條目,僅在每次必要的數據操作完成后進行編碼

替代品,但笨拙(可怕):

  • json_decode ,添加東西, json_encode
  • 將您的附加數據構建為字符串, str_replace例如"area": "Area1" json字符串中的"area": "Area1", "image": "1278.jpg""area": "Area1", "image": "1278.jpg"

但實際上:輸出格式如json_encode應該只在你確定你有整個輸出並且發送出去后才能完成

PHP對JSON不是很好。 最好從JSON轉換為Array來做到這一點 - @ egig也推薦了。

示例代碼:

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

希望這可以幫助。

也許事情已經發生了變化,但我知道這將在目前的階段發揮作用: -

所以這是JSON字符串: -

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

如果這仍然是一個字符串,那么我會把它轉換成這樣的對象: -

$json = json_decode( $strJSON );

現在它是對象格式,要添加我的“圖像”鍵及其值,我會添加它們,如下所示: -

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

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

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

然后我們應該看到它在頁面上輸出如下: -

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
        )

)

// $ index =數組中的某個值;

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM