[英]json format with json_encode()
I have been working with json_encode() in PHP and the output is: 我一直在使用PHP中的json_encode(),输出为:
{"Inventors:":"Wilson; Nestor Antonio Lagos (Santiago, CL)"}
{"Applicant: ":"Nestor Antonio Lagos Santiago N\/A CL"}
{"Name":"Type"}
And I want the json format like as: 我想要像这样的json格式:
json = {
"Inventors:":"Wilson; Nestor Antonio Lagos (Santiago, CL)",
"Applicant: ":"Nestor Antonio Lagos Santiago N\/A CL",
"Name":"Type"
};
I've tried but always the convertion of my array is the same. 我已经尝试过了,但是数组的转换总是一样的。
$contents = "<table><tr><td>Row 1 Column 1</td><td>Row 1 Column 2</td></tr><tr><td>Row 2 Column 1</td><td>Row 2 Column 2</td></tr></table>";
$DOM = new DOMDocument;
$DOM->loadHTML($contents);
$items = $DOM->getElementsByTagName('tr');
function tdrows($elements)
{
$str = "";
$i = 0;
foreach ($elements as $element)
{
if($i == 0) {
$itm = $element->nodeValue;
$i++;
} else {
if(strlen($element->nodeValue) > 1 )
$array = array($itm => trim($element->nodeValue));
}
//$str .= $element->nodeValue . ", ";
}
echo json_encode($array);
}
You can create required json output using following snippet: 您可以使用以下代码段创建所需的json输出:
<?php
$people = array();
$people['Inventors']='Wilson; Nestor Antonio Lagos (Santiago, CL)';
$people['Applicant']='Nestor Antonio Lagos Santiago N\/A CL';
$people['Name']='Nestor Antonio Lagos Santiago N\/A CL';
echo json_encode($people);
?>
You are receiving output like that because you are creating a new array and inserting into $array. 之所以会收到这样的输出,是因为您正在创建一个新数组并将其插入$ array中。 Use following code 使用以下代码
$array[$itm] = trim($element->nodeValue);
and it will work. 它会工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.