0

我有一个要解析为小对象数组的 json 字符串,我正在为此使用解码器,但它无济于事,为什么会这样?

我已将变量定义为 $cleanforcharacters

$cleanforcharacters = preg_replace('/["{mtwrdayfsasusseto}"]_/', '', $found['newdiscounthours']);

这是我的输出

discount_org: "{"day":"8:00","time":"12:00","discount":"10","day":"8:00","time":"12:00","discount":"10"}"

这是所需的输出(对象数组)

discount_org: [
{
day: 0,
time: 8,
discount: 10
},
{
day: 0,
time: 14,
discount: 10
},

这就是我尝试的方式

$arrayOfEmails = json_decode($cleanforcharacters);

这就是我现在得到的

discount_org: {
day: "20",
time: "12:00",
discount: "20"
}

其余的也没有出来

4

1 回答 1

0

这是因为您已将其声明为对象,并且键正在覆盖值而不是被设置为新值:-

你已经给了:-

discount_org: "{"day":"8:00","time":"12:00","discount":"10","day":"8:00","time":"12:00","discount":"10"}"

它应该是:-

discount_org: "[{"day":"8:00","time":"12:00","discount":"10"},{"day":"8:00","time":"12:00","discount":"10"}]"

然后使用: -

$arrayOfEmails = json_decode($cleanforcharacters,true);

这会给你正确的结果。

于 2013-07-19T07:17:04.857 回答