簡體   English   中英

形成一個 JSON 得到一個多維數組 PHP

[英]Form a JSON get an multidimensional array in PHP

我收到這樣的 json:

[{"name":"banana","country":"Algeria"},{"name":"Apple","country":"China"}]

如何將其轉換為數組,其中鍵是name

我正在使用json_decode($json, true); 但它會創建一個自動增量鍵。

所以我想要這樣的東西:

Array
(
    [banana] => Array
        (
            [country] => Algeria
        )
    [Apple] => Array
        (
            [country] => China
        )
)

謝謝。

你的意思是這樣的?

json 代碼:

{
    "Banana": {
        "country":"Algeria"
    },
    "Apple": {
        "country":"China"
    }
}

php 代碼:

    <?php
        $fruitsandcountries = json_decode(file_get_contents("filename.json"), true);
        echo($fruitsandcountries['Banana']['country']);
    ?>

output:

阿爾及利亞

暫無
暫無

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

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