简体   繁体   中英

formatted json format in php webservices

I am new in PHP. I am trying to format JSON. Here's the relevant code:

$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);

I'm getting this output:

{
  "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"
        }
      }
    }]
}

But I want it to be:

{
    "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"
            }
        }
    }]
}

What can I do to get the above output?

just change this part:

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

to

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

Just copy and paste this code

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));
}

Please change

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

My guess is that there is a problem with associative arrays and the way you are packing it. You can try to organize your response array like this:

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

It should work, didn't check it out thou.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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