php while循环使用数组替换json输出

时间:2018-02-04 09:56:28

标签: php json sql-server xamarin.forms

我的PHP代码如下

<?php

require_once(dirname(__FILE__).'/connectionInfoTestNew.php');


$connectionInfo = new ConnectionInfo();
$connectionInfo->GetConnection();

if (!$connectionInfo->conn)
{
    //Connection failed
    echo 'No Connection';
}

else
{

    $query = 'select DISTINCT i.tabledetailid as tabledetailid, i.name as name, c.tabledetailid as tableDet from tabledetail i left join temporderdetail c on i.tabledetailid = c.tabledetailid';

    $stmt = sqlsrv_query($connectionInfo->conn, $query);

    if (!$stmt)
    {
        //Query failed
        echo 'Query failed';
    }

    else
    {
        $contacts = array(); //Create an array to hold all of the contacts

        while ($row = sqlsrv_fetch_array($stmt,SQLSRV_FETCH_ASSOC)) //While there are still contacts
        {               
            //the names must match exactly the property names in the contact class in our C# code.
            $contact= array("ID" => $row['tabledetailid'],
                             "Name" => $row['name'],
                             "tableDet" => $row['tableDet']);

            //Add the contact to the contacts array
            array_push($contacts, $contact);
        }

        //Echo out the contacts array in JSON format
        header('Content-type: application/json');
        $output = ['TableInfo' => $contacts];
        echo json_encode($output, JSON_PRETTY_PRINT);
    }
}?>

,输出如下

{
"TableInfo": [
    {
        "ID": "1",
        "Name": "TABLE 01",
        "tableDet": "1"
    },
    {
        "ID": "2",
        "Name": "TABLE 02",
        "tableDet": "2"
    },
    {
        "ID": "3",
        "Name": "TABLE 03",
        "tableDet": null
    },
    {
        "ID": "4",
        "Name": "TABLE 04",
        "tableDet": null
    },
    {
        "ID": "5",
        "Name": "TABLE 05",
        "tableDet": "5"
    },
    {
        "ID": "6",
        "Name": "TABLE 06",
        "tableDet": null
    },
    {
        "ID": "7",
        "Name": "TABLE 07",
        "tableDet": null
    },
    {
        "ID": "8",
        "Name": "TABLE 08",
        "tableDet": "8"
    },
    {
        "ID": "9",
        "Name": "TABLE 09",
        "tableDet": "9"
    },
    {
        "ID": "10",
        "Name": "TABLE 10",
        "tableDet": null
    },
    {
        "ID": "11",
        "Name": "TABLE 11",
        "tableDet": null
    },
    {
        "ID": "12",
        "Name": "TABLE 12",
        "tableDet": null
    },
    {
        "ID": "13",
        "Name": "TABLE 13",
        "tableDet": null
    },
    {
        "ID": "14",
        "Name": "TABLE 14",
        "tableDet": null
    },
    {
        "ID": "15",
        "Name": "TABLE 15",
        "tableDet": null
    },
    {
        "ID": "16",
        "Name": "TABLE 01",
        "tableDet": null
    }
]}

我的期望是替换“tableDet”:“2”和“tableDet”:“3”等.....作为“tableDet”:“被占用”的例子期待下面的输出提到

{
"TableInfo": [
    {
        "ID": "1",
        "Name": "TABLE 01",
        "tableDet": "occupied"
    },
    {
        "ID": "2",
        "Name": "TABLE 02",
        "tableDet": "occupied"
    },
    {
        "ID": "3",
        "Name": "TABLE 03",
        "tableDet": null
    },
    {
        "ID": "4",
        "Name": "TABLE 04",
        "tableDet": null
    },
    {
        "ID": "5",
        "Name": "TABLE 05",
        "tableDet": "occupied"
    }]}

那么如何将数字输出(1,2,3等)替换为php代码中的字符串输出(“占用”)? ,预先感谢您的支持

1 个答案:

答案 0 :(得分:1)

在循环中尝试:

if(isset($row['tableDet'])){// or !is_null() or is_numeric()
$tableDet = "occupied"; 
}else{
$tableDet = $row['tableDet']);//or = "free" or null
}
$contact= array(
     "ID" => $row['tabledetailid'],
      "Name" => $row['name'],
      "tableDet" => $tableDet);