我有这个字符串:
[{"position":"1d","number":10,"nb_plot1":12,"max_actions":3}
{"position":"2d","number":7,"nb_plot1":15,"max_actions":30}
{"position":"3d","number":100,"nb_plot1":2,"max_actions":5}]
我需要获得两个不同格式的字符串,如下所示:
代表数字:
[10,7,100]
职位:
['1d','2d','3d']
并删除不必要的字符串。
我很抱歉。
答案 0 :(得分:2)
使用json_decode解码数组中的字符串。
迭代此数组并为数字和位置构建新数组。
Encode数组。
在代码中:
$arr = json_decode($string);
$numbers = array();
$positions = array();
foreach($arr as $a)
{
$numbers[] = (int)$a->number;
$positions[] = $a->position;
}
$number_string = json_encode($numbers);
$position_string = json_encode($positions);