如何解码json字符串并在PHP中添加到数组

时间:2015-04-16 03:29:30

标签: php mysql recursion

我有一些来自数据库的记录,我获取了所有记录。该数组返回如下代码

array (size=10)
  0 => 
    array (size=5)
      'id' => string '1' (length=1)
      'type' => string 'group' (length=5)
      'members' => null
      'parents' => string '0' (length=1)
      'level' => string '1' (length=1)
  1 => 
    array (size=5)
      'id' => string '2' (length=1)
      'type' => string 'group' (length=5)
      'members' => null
      'parents' => string '0' (length=1)
      'level' => string '1' (length=1)
  2 => 
    array (size=5)
      'id' => string '3' (length=1)
      'type' => string 'team' (length=4)
      'members' => string '["3","5","6"]' (length=13)
      'parents' => string '1' (length=1)
      'level' => string '2' (length=1)
  3 => 
    array (size=5)
      'id' => string '4' (length=1)
      'type' => string 'team' (length=4)
      'members' => string '["1"]' (length=5)
      'parents' => string '1' (length=1)
      'level' => string '2' (length=1)
  4 => 
    array (size=5)
      'id' => string '5' (length=1)
      'type' => string 'group' (length=5)
      'members' => null
      'parents' => string '1' (length=1)
      'level' => string '2' (length=1)
  5 => 
    array (size=5)
      'id' => string '8' (length=1)
      'type' => string 'team' (length=4)
      'members' => string '["4"]' (length=5)
      'parents' => string '5' (length=1)
      'level' => string '3' (length=1)
  6 => 
    array (size=5)
      'id' => string '9' (length=1)
      'type' => string 'team' (length=4)
      'members' => string '["2"]' (length=5)
      'parents' => string '5' (length=1)
      'level' => string '3' (length=1)
  7 => 
    array (size=5)
      'id' => string '10' (length=2)
      'type' => string 'team' (length=4)
      'members' => null
      'parents' => string '5' (length=1)
      'level' => string '3' (length=1)
  8 => 
    array (size=5)
      'id' => string '11' (length=2)
      'type' => string 'team' (length=4)
      'members' => null
      'parents' => string '5' (length=1)
      'level' => string '3' (length=1)
  9 => 
    array (size=5)
      'id' => string '12' (length=2)
      'type' => string 'group' (length=5)
      'members' => null
      'parents' => string '1' (length=1)
      'level' => string '2' (length=1)

成员字段已编码为json字符串。我想构建一个格式为下面代码的数组

 array('1'=>array(1,2,3,4,5,6),
       '2'=>array(),
       '3'=>array(3,5,6),
       '4'=>array(1),
       '5'=>array(2,4),
       '8'=>array(4),
       '9'=>array(2))

记录的id将是数组的键,成员已解码将成为值。我写了一个函数句柄从数据库返回数组但结果不是我的意图。这是我的代码

$results = mysql_query('select id,type,members,parents,level from team'); 
$array = array();
recursiveDate($results,0,$array);

function recursiveData($sourceArr,$parents = 0, &$newMenu){
        if(count($sourceArr)>0){
            foreach ($sourceArr as $key => $value){
                if($value['parents'] == $parents){
                    if(isset($newMenu[$value['id']])){
                        $newMenu[$value['id']] = array(); 
                    }   
                    $newMenu[$value['id']]  = $value['members']?json_decode($value['members']):array();
                    if(isset($newMenu[$parents])){
                        $newMenu[$parents] = array_merge($newMenu[$value['id']],$newMenu[$parents]);
                    }


                    $newParents = $value['id'];
                    unset($sourceArr[$key]);
                    $this->recursiveData($sourceArr,$newParents, $newMenu);
                }
            }
        }
    }

这是处理后的数组

array (size=10)
  1 => 
    array (size=4)
      0 => string '1' (length=1)
      1 => string '3' (length=1)
      2 => string '5' (length=1)
      3 => string '6' (length=1)
  3 => 
    array (size=3)
      0 => string '3' (length=1)
      1 => string '5' (length=1)
      2 => string '6' (length=1)
  4 => 
    array (size=1)
      0 => string '1' (length=1)
  5 => 
    array (size=2)
      0 => string '2' (length=1)
      1 => string '4' (length=1)
  8 => 
    array (size=1)
      0 => string '4' (length=1)
  9 => 
    array (size=1)
      0 => string '2' (length=1)
  10 => 
    array (size=0)
      empty
  11 => 
    array (size=0)
      empty
  12 => 
    array (size=0)
      empty
  2 => 
    array (size=0)
      empty

请帮我构建那个数组

1 个答案:

答案 0 :(得分:0)

我很快就写下了这个,所以不确定它是不是你正在寻找的东西,看起来你已经非常复杂了。我保留了您想要的原始结果结构,但如果您愿意,可以轻松覆盖它。

function modifyData($data = array()) {
    foreach($data as &$entry) {
        if(!empty($entry['members'])) {
            $entry['members'] = json_decode($entry['members'], true);
        } else {
            $entry['members'] = array();
        }
    }
    return $data;
}

我用

测试了这个
$data = array(
    array(
        'id' =>'1',
        'type' =>'group',
        'members' => null,
        'parents' =>'0',
        'level' =>'1'
    ),
    array(
        'id' =>'1',
        'type' =>'group',
        'members' => '["3","5","6"]',
        'parents' =>'0',
        'level' =>'1'
    ),
    array(
        'id' =>'1',
        'type' =>'group',
        'members' => '["3"]',
        'parents' =>'0',
        'level' =>'1'
    )
);

输出

array (size=3)
  0 => 
    array (size=5)
      'id' => string '1' (length=1)
      'type' => string 'group' (length=5)
      'members' => 
        array (size=0)
          empty
      'parents' => string '0' (length=1)
      'level' => string '1' (length=1)
  1 => 
    array (size=5)
      'id' => string '1' (length=1)
      'type' => string 'group' (length=5)
      'members' => 
        array (size=3)
          0 => string '3' (length=1)
          1 => string '5' (length=1)
          2 => string '6' (length=1)
      'parents' => string '0' (length=1)
      'level' => string '1' (length=1)
  2 => 
    array (size=5)
      'id' => string '1' (length=1)
      'type' => string 'group' (length=5)
      'members' => 
        array (size=1)
          0 => string '3' (length=1)
      'parents' => string '0' (length=1)
      'level' => string '1' (length=1)