在PHP

时间:2017-02-17 08:54:43

标签: php

拥有以下字符串:

"[{"name":null,"value":"","target":null,"alias":"","required":1,"showNull":0}]"

如何从1部分获取required?我只需要1

提前致谢。

3 个答案:

答案 0 :(得分:3)

假设字符串始终为JSON,请使用json_decode()

$array = json_decode('[{"name":null,"value":"","target":null,"alias":"","required":1,"showNull":0}]');

现在您可以按如下方式访问required选项:

$array[0]->required;

答案 1 :(得分:0)

您可以使用json_decode

<?php
$string = '[{"name":null,"value":"","target":null,"alias":"","required":1,"showNull":0}]';
$string_array = json_decode($string, true);
echo $string_array[0]['required'];
?>

答案 2 :(得分:0)

使用函数json_decodecurrent

在单行中
$str = '[{"name":null,"value":"","target":null,"alias":"","required":1,"showNull":0}]';
$required = current(json_decode($str))->required;