我正在开发一个发送 id 的应用程序,作为回报,php 向客户端发送一个 json 节点。我的问题是我应该将数据保存为静态 php 数组,如底部代码所示,还是将数据保存到外部 xml/json 文件中,还是应该使用数据库?数组中可能有多达 10,000 个项目。请建议。
<?php
// Fill up array with names
$a = array(
array("id"=>1001, "name"=>"One", "address"=>"", "city"=>"", "state"=>""),
array("id"=>1002, "name"=>"Two", "address"=>"", "city"=>"", "state"=>""),
array("id"=>1003, "name"=>"Three", "address"=>"", "city"=>"", "state"=>""),
array("id"=>1004, "name"=>"Four", "address"=>"", "city"=>"", "state"=>""),
array("id"=>1005, "name"=>"Five", "address"=>"", "city"=>"", "state"=>""),
array("id"=>1006, "name"=>"Six", "address"=>"", "city"=>"", "state"=>"")
);
//get the q parameter from URL
$q=$_GET["q"];
//lookup all hints from array if length of q>0
if (strlen($q) > 0)
{
$hint="";
$att = "id";
for($i=0; $i<count($a); $i++)
{
if($q==$a[$i][$att])
{
echo json_encode($a[$i]);
}
}
}
echo "";
?>