Answers:
轻松榨柠檬汁:http ://www.php.net/manual/zh/function.json-encode.php
<?php
$arr = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5);
echo json_encode($arr);
?>
andyrusterholz at g-m-a-i-l dot c-o-m
在上述页面上有一篇文章,也可以处理复杂的嵌套数组(如果那是您的事)。
使用PHP的native json_encode
,如下所示:
<?php
$arr = array(
array(
"region" => "valore",
"price" => "valore2"
),
array(
"region" => "valore",
"price" => "valore2"
),
array(
"region" => "valore",
"price" => "valore2"
)
);
echo json_encode($arr);
?>
更新:在评论中回答您的问题。您可以这样操作:
$named_array = array(
"nome_array" => array(
array(
"foo" => "bar"
),
array(
"foo" => "baz"
)
)
);
echo json_encode($named_array);
简单:只需创建一个(嵌套的)PHP数组并对其进行调用json_encode
。数字数组转换为JSON列表([]
),关联数组和PHP对象转换为对象({}
)。例:
$a = array(
array('foo' => 'bar'),
array('foo' => 'baz'));
$json = json_encode($a);
给你:
[{"foo":"bar"},{"foo":"baz"}]
每次在php中创建json时最好的方法是首先转换ASSOCIATIVE数组中的值。
之后,只需使用即可编码json_encode($associativeArray)
。我认为这是在php中创建json的最佳方法,因为每当大多数时候我们都在php中从sql查询结果表单时,我们都会使用fetch_assoc
函数获取值,该函数还会返回一个关联数组。
$associativeArray = array();
$associativeArray ['FirstValue'] = 'FirstValue';
...等
之后。
json_encode($associativeArray);
<?php
$username=urldecode($_POST['log_user']);
$user="select * from tbl_registration where member_id= '".$username."' ";
$rsuser = $obj->select($user);
if(count($rsuser)>0)
{
// (Status if 2 then its expire) (1= use) ( 0 = not use)
$cheknew="select name,ldate,offer_photo from tbl_offer where status=1 ";
$rscheknew = $obj->selectjson($cheknew);
if(count($rscheknew)>0)
{
$nik=json_encode($rscheknew);
echo "{\"status\" : \"200\" ,\"responce\" : \"201\", \"message\" : \"Get Record\",\"feed\":".str_replace("<p>","",$nik). "}";
}
else
{
$row2="No Record Found";
$nik1=json_encode($row2);
echo "{\"status\" : \"202\", \"responce\" : \"604\",\"message\" : \"No Record Found \",\"feed\":".str_replace("<p>","",$nik1). "}";
}
}
else
{
$row2="Invlid User";
$nik1=json_encode($row2);
echo "{\"status\" : \"404\", \"responce\" : \"602\",\"message\" : \"Invlid User \",\"feed\":".str_replace("<p>","",$nik1). "}";
}
?>
我创建了一个简单的jsonOBJ类,用于我的代码。PHP不像JavaScript / Node一样包含json函数。您必须进行不同的迭代,但可能会有所帮助。
<?php
// define a JSON Object class
class jsonOBJ {
private $_arr;
private $_arrName;
function __construct($arrName){
$this->_arrName = $arrName;
$this->_arr[$this->_arrName] = array();
}
function toArray(){return $this->_arr;}
function toString(){return json_encode($this->_arr);}
function push($newObjectElement){
$this->_arr[$this->_arrName][] = $newObjectElement; // array[$key]=$val;
}
function add($key,$val){
$this->_arr[$this->_arrName][] = array($key=>$val);
}
}
// create an instance of the object
$jsonObj = new jsonOBJ("locations");
// add items using one of two methods
$jsonObj->push(json_decode("{\"location\":\"TestLoc1\"}",true)); // from a JSON String
$jsonObj->push(json_decode("{\"location\":\"TestLoc2\"}",true));
$jsonObj->add("location","TestLoc3"); // from key:val pairs
echo "<pre>" . print_r($jsonObj->toArray(),1) . "</pre>";
echo "<br />" . $jsonObj->toString();
?>
将输出:
Array
(
[locations] => Array
(
[0] => Array
(
[location] => TestLoc1
)
[1] => Array
(
[location] => TestLoc2
)
[2] => Array
(
[location] => TestLoc3
)
)
)
{"locations":[{"location":"TestLoc1"},{"location":"TestLoc2"},{"location":"TestLoc3"}]}
要进行迭代,请转换为普通对象:
$myObj = $jsonObj->toArray();
然后:
foreach($myObj["locations"] as $locationObj){
echo $locationObj["location"] ."<br />";
}
输出:
TestLoc1
TestLoc2
TestLoc3
直接访问:
$location = $myObj["locations"][0]["location"];
$location = $myObj["locations"][1]["location"];
一个实际的例子:
// return a JSON Object (jsonOBJ) from the rows
function ParseRowsAsJSONObject($arrName, $rowRS){
$jsonArr = new jsonOBJ($arrName); // name of the json array
$rows = mysqli_num_rows($rowRS);
if($rows > 0){
while($rows > 0){
$rd = mysqli_fetch_assoc($rowRS);
$jsonArr->push($rd);
$rows--;
}
mysqli_free_result($rowRS);
}
return $jsonArr->toArray();
}