2012-04-25 23 views
1

之间的过滤器我有这个字符串:从一个字符串中两个,用绳子

[{"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'] 

并剪下不必要的字符串。

我是一个noob抱歉。

+1

http://php.net/manual/en/function.json-decode.php你会发现这是很有帮助的 – Dave 2012-04-25 19:51:24

+0

你想获得两个阵列不串 – mgraph 2012-04-25 19:51:42

+0

@戴夫@mgraph谢谢! – user1147636 2012-04-25 19:54:50

回答

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); 
+2

为什么不直接在最后一步使用'json_encode'? :) – 2012-04-25 19:58:53

+0

哦,对。 :D谢谢! @丹李 – 2012-04-25 20:09:32

+0

@戈尔如果我把var_dump(json_decode($ string,true));看到数组,但用你的代码我有一个空白页 – user1147636 2012-04-25 20:39:31

相关问题