I'm trying to add a search bar to a UI Kit template. I have a JSON file that I want PHP to parse to only show a subset of the JSON file.
I have an example of what the JSON code needs to look like: 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;
?>
I've tried several configurations without any luck.
Final Answer:
<?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;
?>
If you want such structure, you may need to remove the sub keys, google, microsoft, etc..
to make a similar match. Consider this example:
$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>';
This yields something like:
{
"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"
}
]
}
Similar to your link. Try to remove the sub keys.