我正在尝试将搜索栏添加到UI Kit模板。我有一个JSON文件,我希望PHP解析为只显示JSON文件的一个子集。
我有一个JSON代码需要的示例:Example
<?php
$json = json_encode(array(
"results" => array(
array(
"title" => "Google",
"url" => "http://google.com",
"text" => "A large search engine"),
array(
"title" => "Microsoft",
"url" => "http://microsoft.com",
"text" => "Devices and Services company"),
array(
"title" => "Apple",
"url" => "http://apple.com",
"text" => "iPad, iPhone, Mac, iOS"),
array(
"title" => "IBM",
"url" => "http://ibm.com",
"text" => "Innovators of hardware and software")
)
));
$_REQUEST['search'] = $json;
?>
我尝试了几种配置而没有任何运气。
最终答案:
<?php
if ((isset($_REQUEST['search'])) && (!empty($_REQUEST['search'])))
{
$search_field= $_REQUEST['search'];
}
$json = json_encode(array(
"results" => array(
array(
"title" => "Google",
"url" => "http://google.com",
"text" => "A large search engine"),
array(
"title" => "Microsoft",
"url" => "http://microsoft.com",
"text" => "Devices and Services company"),
array(
"title" => "Apple",
"url" => "http://apple.com",
"text" => "iPad, iPhone, Mac, iOS"),
array(
"title" => "IBM",
"url" => "http://ibm.com",
"text" => "Innovators of hardware and software")
)
));
echo $json;
?>
答案 0 :(得分:0)
如果您需要此类结构,则可能需要删除子键google, microsoft, etc..
以进行类似的匹配。考虑这个例子:
$json = json_encode(array(
"results" => array(
array(
"title" => "Google",
"url" => "http://google.com",
"text" => "A large search engine"),
array(
"title" => "Microsoft",
"url" => "http://microsoft.com",
"text" => "Devices and Services company"),
array(
"title" => "Apple",
"url" => "http://apple.com",
"text" => "iPad, iPhone, Mac, iOS"),
array(
"title" => "IBM",
"url" => "http://ibm.com",
"text" => "Innovators of hardware and software")
)
), JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
echo '<pre>';
print_r($json);
echo '</pre>';
这会产生类似的结果:
{
"results": [
{
"title": "Google",
"url": "http://google.com",
"text": "A large search engine"
},
{
"title": "Microsoft",
"url": "http://microsoft.com",
"text": "Devices and Services company"
},
{
"title": "Apple",
"url": "http://apple.com",
"text": "iPad, iPhone, Mac, iOS"
},
{
"title": "IBM",
"url": "http://ibm.com",
"text": "Innovators of hardware and software"
}
]
}
与您的链接类似。尝试删除子键。