带有数组替换json输出的php while循环
我的php代码如下带有数组替换json输出的php while循环,php,json,sql-server,xamarin.forms,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'; } el
<?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);
}
}?>
我的期望是替换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代码中的字符串OutputOccuped,提前感谢您的支持在循环中尝试以下内容:
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);
将tableDet=>$row['tableDet']替换为tableDet=>isset$row['tableDet']?'占用:空。非常感谢。它工作得很好。很好。也许我应该解释一下…?:。。。叫做三元运算符,看:谢谢链接,真的很有帮助
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);