简体   繁体   中英

php while loop with array replace json output

My php code as follows

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

and the output as follows

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

what my expectation is that to replace the "tableDet": "2" and "tableDet": "3" Etc..... as "tableDet": "Occupied" example expecting output mention below

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

so how should i replace the number output ( 1,2,3,etc) to the string output("occupied") in the php code ? , thank you in-advance for your support

Try this inside the loop:

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

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