php这两组数组怎么取到data里的值?

例如,在方法里,

    public function a(){
      $one =  $this->b();
      $two =  $this->c();
    }
    
    private function b(){
      ...
    return data;
    }
    
    private function c(){
      ...
    return data;
    }

现在假设$one的数据格式是这样的`{'status':true, 'data': [{'a':'a1','b': 'b1'}, {'c':'c1', d':'d1'}]}`
$two的数据格式是这样`{'status':true, 'data': [{'e': 'e1', 'f': 'f1'}, {'g': 'g1', 'h': 'h1'}]}`

请问这`function a` 里怎么把$one和$two里的data值合并成下面这样:

    {'status':true, 'data': [{'a':'a1','b': 'b1','e': 'e1', 'f': 'f1'}, {'c':'c1', d':'d1', 'g': 'g1', 'h': 'h1'}]}
阅读 3.4k
2 个回答

你这数据是怎么来的,标准json不是应该用双引号么,你这单引号....单双引号用str_replace替换

function a(){
    $one =  json_decode('{"status":true,"data":[{"a":"a1","b":"b1"},{"c":"c1","d":"d1"}]}',true);
    $two =  json_decode('{"status":true,"data":[{"e":"e1","f":"f1"},{"g":"g1","h":"h1"}]}',true);
    echo json_encode(array('status'=>true,'data'=>array_map("myfunction",$one['data'],$two['data'])));
}
a();
function myfunction($v1,$v2){ return array_merge($v1,$v2); }

php函数满足不了就自己遍历拼接么

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题