簡體   English   中英

需要使用 php json_encode 的 json 數組

[英]Need array in json with php json_encode

我有一個這樣的數組:

$priceArray = [
            'pricing' => [
                'Prices' => [
                   'quantity' => 1,
                   'price' => floatval($price)
                ]
           ]
        ];

在 json_encode 之后它給了我:

{"pricing":
     {"Prices":
        {"quantity":1,
          "price":24
        }
    }
}

但是我需要 :

{
    "pricing":
       {"Prices":[
           {"quantity":1,
              "price":24
           }
      ]
   }
}

我怎樣才能讓 Prices 元素是一個數組而不是一個對象?

當前,數組的Prices元素是單個鍵索引數組。 要產生您想要的結果,它需要是一個鍵索引數組。

$priceArray = [
        'pricing' => [
            'Prices' => [
               [
                   'quantity' => 1,
                   'price' => floatval($price)
               ]
            ]
       ]
    ];

演示

$priceArray = [
    'pricing' => [
        'Prices' => [[
           'quantity' => 1,
           'price' => floatval($price)
        ]]
   ]
];

這將產生

{
    "pricing": {
        "Prices": [{
            "quantity": 1,
            "price": 4
        }]
    }
}

您只需將Prices數據包裝在另一個數組中。

暫無
暫無

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

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