dsghpgmay31938863 2014-06-06 00:29
浏览 29
已采纳

形成UI Kit搜索JSON

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;
?>
  • 写回答

1条回答 默认 最新

  • doushi4956 2014-06-06 00:37
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!