【问题标题】:php while loop with array replace json outputphp while循环用数组替换json输出
【发布时间】:2018-02-04 09:56:27
【问题描述】:

我的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”:“Occupied”示例,期望下面提到的输出

{
"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 代码中的字符串输出(“occupied”)? , 提前感谢您的支持

【问题讨论】:

  • "tableDet" =&gt; $row['tableDet'] 替换为"tableDet" =&gt; (isset($row['tableDet']) ? 'occupied' : NULL)
  • 非常感谢它的完美运行
  • 那很好。也许我应该解释一下 ... ? ... : ... 被称为 三元运算符,请参阅:php.net/manual/en/…
  • 谢谢你的链接真的很有帮助

标签: php json sql-server xamarin.forms


【解决方案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);

【讨论】:

    猜你喜欢
    • 2012-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-21
    • 1970-01-01
    • 2015-01-21
    相关资源
    最近更新 更多