dqwh0108 2017-06-19 14:37
浏览 46
已采纳

返回字符串json编码的php文件没有用

I have an php file that gives me the response i need as a string, i need it to be an array encoded in json, im not that good with php, can any one help ? this is how it works fine as a string - printf("user: \"%s\" \"%s\" email: \"%s\" ", $row['firstname'], $row['lastname'],$row['email']);

But i need it as an array in json that is how i tried to do it -array_push($mynewArray,array("firstname \%s"=>$row['firstname'],"lastname \%s"=>$row['lastname'],"email \%s"=>$row['email'])); echo json_encode($mynewArray);

this is the screen shot that shows how i tried to do it

  • 写回答

2条回答 默认 最新

  • dongshi4589 2017-06-19 14:45
    关注

    I think you could try like this assuming the recordset is generated correctly and available in the var $row

    $mynewArray=array();
    foreach($result as $row){
        $mynewArray[]=$row;
    }
    echo json_encode($mynewArray);
    
    
    Or
    
    $mynewArray=array();
    foreach( $result as $row ){
        $mynewArray[]=array(
            'firstname'=>$row['firstname'],
            'lastname'=>$row['lastname'],
            'email'=>$row['email'],
        );
    }
    echo json_encode($mynewArray);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?