繁体   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