0

如果我从 db 表( Mysql )中读取数据,我会得到这个作为返回:

[{"name":"Mobile Groups","value":1,"target":null,"alias":"mb_group","required":1,"showNull":0},
{"name":"Beta Group","value":3,"target":null,"alias":"mb_group","required":1,"showNull":0},
{"name":"Delta Group","value":4,"target":null,"alias":"mb_group","required":1,"showNull":0}]

我无法在任何地方找到这是什么数据类型,但那不是数组,对吗?我可以将此数据转换为数组吗,我的复选框和单选按钮需要此数据,因为我将使用foreach控制它,这可能吗?

4

2 回答 2

1
$string = '[{"name":"Mobile Groups","value":1,"target":null,"alias":"mb_group","required":1,"showNull":0},
{"name":"Beta Group","value":3,"target":null,"alias":"mb_group","required":1,"showNull":0},
{"name":"Delta Group","value":4,"target":null,"alias":"mb_group","required":1,"showNull":0}]';

$array = json_decode($string);

var_dump($array);

输出

array (size=3)
  0 => 
    object(stdClass)[1]
      public 'name' => string 'Mobile Groups' (length=13)
      public 'value' => int 1
      public 'target' => null
      public 'alias' => string 'mb_group' (length=8)
      public 'required' => int 1
      public 'showNull' => int 0
  1 => 
    object(stdClass)[2]
      public 'name' => string 'Beta Group' (length=10)
      public 'value' => int 3
      public 'target' => null
      public 'alias' => string 'mb_group' (length=8)
      public 'required' => int 1
      public 'showNull' => int 0
  2 => 
    object(stdClass)[3]
      public 'name' => string 'Delta Group' (length=11)
      public 'value' => int 4
      public 'target' => null
      public 'alias' => string 'mb_group' (length=8)
      public 'required' => int 1
      public 'showNull' => int 0

你也可以得到带有TRUE参数的数组转换对象:

$array = json_decode($string, true);
于 2013-08-16T09:40:20.317 回答
0

对我来说看起来像JSON,更具体地说是三个对象的 JSON 数组。

在 PHP 中,你应该能够json_decode()做到。

于 2013-08-16T09:37:48.350 回答