拆分字符串中的多个单词实例

时间:2017-05-17 20:32:45

标签: php

我有这样的字符串:

$T = '[{"X":"13","Y":"76", "R":"90"},{"X":"12","Y":"24","R":"-19"}]}';

$A = '[{"X":"1","Y":"6", "R":"0"},{"X":"1","Y":"4","R":"9"},
    {"X":"12","Y":"24","R":"-19"}]}';

我想在字符串中找到每个X值,然后获取它们之后的值。

请注意,X值的数量不同。

例如,我想:

13, 12, 1, 1, 12

我尝试了explode(),但只抓住了第一个X值(13)。我是PHP的新手,并不知道一个优雅的解决方案。任何建议都表示赞赏。

1 个答案:

答案 0 :(得分:0)

删除字符串的最后一个大括号(})以使其有效json,即:

$A = '[{"X":"1","Y":"6", "R":"0"},{"X":"1","Y":"4","R":"9"},
    {"X":"12","Y":"24","R":"-19"}]}';
$A = trim($A, '}');
$json = json_decode($A, true);
foreach($json as $value)
{
    echo $value['X'];
}
# 1112