繁体   English   中英

PHP WebServices中的格式化json格式

[英]formatted json format in php webservices

我是PHP新手。 我正在尝试格式化JSON。 以下是相关代码:

$query = mysql_query("SELECT * 
FROM  `micards` m
JOIN user u ON m.`mobile` = u.phone");

while ($row = mysql_fetch_assoc($query)) {
    $response["success"] = 1;
    $name = array('name'=>$row["name"],'email'=>$row["email"],'mobile'=>$row["mobile"]);
    $phone[] = array('phone'=>$row["phone"],array('card'=>$name));
    $response["contacts"] = $phone;
}
echo json_encode($response);

我得到以下输出:

{
  "success": 1,
    "contacts": [{
      "phone": "919898989898",
      "0": {
        "card": {
          "name": "abcd",
          "email": "vwxy@test.com",
          "mobile": "919898989898"
        }
      }
    }, {
      "phone": "919898989898",
      "0": {
        "card": {
          "name": "abcd",
          "email": "rstp@test.com",
          "mobile": "919898989898"
        }
      }
    }, {
      "phone": "8686868686",
      "0": {
        "card": {
          "name": "pan",
          "email": "pnqr@gmail.com",
          "mobile": "8686868686"
        }
      }
    }, {
      "phone": "8686868686",
      "0": {
        "card": {
          "name": "monk",
          "email": "abcd@gmail.com",
          "mobile": "8686868686"
        }
      }
    }]
}

但我希望它是:

{
    "success": 1,
    "contacts": [{
        "phone": "919898989898",
        {
            "card": {
                "name": "abcd",
                "email": "vwxy@test.com",
                "mobile": "919898989898"
            }
        },
        {
            "card": {
                "name": "abcd",
                "email": "rstp@test.com",
                "mobile": "919898989898"
            }
        }
    }],
    [{
        "phone": "8686868686",
        {
            "card": {
                "name": "panky",
                "email": "pnqr@gmail.com",
                "mobile": "8686868686"
            }
        },
        {
            "card": {
                "name": "panky",
                "email": "abcd@gmail.com",
                "mobile": "8686868686"
            }
        }
    }]
}

我该怎么做才能获得上述输出?

只需更改此部分:

$phone[] = array('phone'=>$row["phone"],array('card'=>$name));    
$response["contacts"] = $phone;

$response["contacts"][] = array('phone'=>$row["phone"],array('card'=>$name));

只需复制并粘贴此代码

while ($row = mysql_fetch_assoc($query)) {
    $response["success"] = 1;
    $name = array('name'=>$row["name"],'email'=>$row["email"],'mobile'=>$row["mobile"]);
    $response["contacts"][] = array('phone'=>$row["phone"],array('card'=>$name));
}

请更换

$phone[] = array('phone'=>$row["phone"],array('card'=>$name)) to 
$phone[] = array('phone'=>$row["phone"],'card'=>$name)
;

我的猜测是关联数组及其打包方式存在问题。 您可以尝试按以下方式组织您的响应数组:

$response = [
    'success'=>1,
    'contacts'=>[
        'phone'=>$row['phone'],
        'card'=>[
            'name'=>$row['name'],
            'email'=>$row['email'],
            'mobile'=>$row['mobile']
        ]
    ]
];

它应该工作,没有检查出来。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM