在特定角色之前和之后获取价值

时间:2016-10-12 09:48:20

标签: php substring

如何在特定字符之后和特定字符之前获取某些值并在php中放入数组

$str = "[{"m":[{"a":1,"u":34,"n":"Administrator"}]},
{"m":[{"a":2,"u":3,"n":"Member"}]},
{"m":[{"a":3,"u":44,"n":"Co-Admin"}]},
{"m":[{"a":2,"u":24,"n":"Member"}]},
{"m":[{"a":2,"u":75,"n":"Member"}]}]"

我希望获取数组中"u":,之间的所有值,结果可能是{'34','3','44','24',75}

2 个答案:

答案 0 :(得分:2)

您可以使用 json_decode 获取值并保存到数组中。

示例 -

$str = '[{"m":[{"a":1,"u":34,"n":"Administrator"}]},{"m":[{"a":2,"u":3,"n":"Member"}]},{"m":[{"a":3,"u":44,"n":"Co-Admin"}]},{"m":[{"a":2,"u":24,"n":"Member"}]},{"m":[{"a":2,"u":75,"n":"Member"}]}]';

 $getArrayJson = json_decode($str);

 //echo "<pre>"; print_r($getArrayJson);

 $requireValueArray = "";
foreach ($getArrayJson as $getkey => $getvalue) {
    $requireValueArray[] = $getvalue->m[0]->u;

}


$yourjsonDecodeArray = json_encode($requireValueArray);

答案 1 :(得分:0)

我得到了解决方案

$json = json_decode($str);
foreach ($json as $jkey) {
    foreach($jkey->m as $row)
    {
        $arrayjson[] = $row->u;
    }
}

echo $ arrayjson [0]将出现34,$ arrayjson [1]将出现3等等感谢@ shubham715为我的脑海中的灯泡呵呵:)