在PHP中创建JSON结构

时间:2014-02-14 17:41:44

标签: php arrays json

$query = "SELECT `contact_id`, `user_id`, `date_backup`, `first_name`, `last_name` FROM tbl_contacts WHERE date(`date_backup`) = (SELECT MAX(date(`date_backup`)) FROM tbl_contacts WHERE `user_id`= '$userId' ) ORDER BY `date_backup` DESC, `contact_id` ASC";
$result = mysql_query($query);
while($row = mysql_fetch_assoc($result)) {
    $contacts = array(array());
    $contactId = $row['contact_id'];
    $names["first_name"] = $row['first_name'];
    $names["last_name"] = $row['last_name'];
    $contacts["names"][] = $names;

    // Phone
    $phoneQuery = "SELECT * FROM tbl_phone_details WHERE contact_id = '$contactId' AND date(`date_backup`) = (SELECT MAX(date(`date_backup`)) FROM tbl_phone_details WHERE `contact_id`= '$contactId')";
    $phoneResult = mysql_query($phoneQuery);

    $total = mysql_affected_rows();
    if($total >= '1') {
        while($phoneRow = mysql_fetch_assoc($phoneResult)) {
            $phones["phone_number"] = $phoneRow['phone_number'];
            $phones["phone_type"] = $phoneRow['phone_type'];
            $contacts["phones"][] = $phones;
        }
    } else {
        $contacts["phones"][] = array();
    }


    // Email    
    $emailQuery = "SELECT * FROM `tbl_email_details` WHERE `contact_id` = '$contactId' AND date(`date_backup`) = (SELECT MAX(date(`date_backup`)) FROM tbl_email_details WHERE `contact_id`= '$contactId')";
    $emailResult = mysql_query($emailQuery);

    $total = mysql_affected_rows();
    if($total >= '1') {
        while($emailRow = mysql_fetch_assoc($emailResult)) {        
            $emails["email_address"] = $emailRow['email_address'];
            $emails["email_type"] = $emailRow['email_type'];
            $contacts["emails"][] = $emails;
        }
    } else {
        $contacts["emails"][] = array();
    }   

    // Address  
    $addressQuery = "SELECT * FROM `tbl_address_detail` WHERE `contact_id` = '$contactId' AND date(`date_backup`) = (SELECT MAX(date(`date_backup`)) FROM tbl_address_detail WHERE `contact_id`= '$contactId')";
    $addressResult = mysql_query($addressQuery);

    $total = mysql_affected_rows();
    if($total >= '1') {
        while($addressRow = mysql_fetch_assoc($addressResult)) {
            $address["street"] = $addressRow['street'];
            $address["city"] = $addressRow['city'];
            $address["state"] = $addressRow['state'];
            $address["zip"] = $addressRow['zip'];
            $address["country"] = $addressRow['country'];
            $address["addressType"] = $addressRow['addressType'];

            $contacts["address"][] = $address;
        }
    } else {
        $contacts["address"][] = array();
    }

    $contectInfoJson["contacts"][] = $contacts;
}

$allContactJson["AllContacts"] = $contectInfoJson;

header('Content-Type: application/json');
echo json_encode($allContactJson);

代码输出: -

AllContacts: {
userId: "15",
contacts: [
{
0: [ ], // Not needed
names: [ // Instead of array i need simple object of names
{
first_name: "ABC",
last_name: "XYZ"
}
],
phones: [
{
phone_number: "+911234567890",
phone_type: "Work"
},
{
phone_number: "+919876543210",
phone_type: "Home"
}
],
emails: [
[ ] //This is also extra and not needed.
],
address: [
{
street: "India",
city: "",
state: "",
zip: "",
country: "",
addressType: ""
}
]
},

必需的输出: -

AllContacts: {
userId: "15",
contacts: [
{
names:
{
first_name: "ABC",
last_name: "XYZ"
},
phones: [
{
phone_number: "+911234567890",
phone_type: "Work"
},
{
phone_number: "+919876543210",
phone_type: "Home"
}
],
emails: [],
address: [
{
street: "India",
city: "",
state: "",
zip: "",
country: "",
addressType: ""
}
]
},

我在代码中面临2-3个小问题。

首先,我在对象的每次启动时都会得到 0:[]

第二个是名称是数组,我希望它是对象而不是数组。

第三是电子邮件:[[]],如果数据不可用,我想要空白数组,但我在数组中获取数组。我只想要电子邮件:[]。

1 个答案:

答案 0 :(得分:1)

  1. $contacts = array(array());替换为$contacts = array();。应该删除0 : []
  2. 可能会触发
  3. $contacts["emails"][] = array();,因此请尝试将其替换为$contacts["emails"] = array();
  4. $contacts["names"][] = $names;替换为$contacts["names"] = $names;