Php 读取JSON文件?

Php 读取JSON文件?,php,json,parsing,Php,Json,Parsing,所以我想用PHP搜索JSON文件。json链接:。我只是把JSON文件放在我的Web服务器中。这是我的剧本: <?php $query = $_GET['s']; $terms = explode(' ', $query); $results = array(); foreach(file('items.json') as $line) { $found = true; foreach($terms as $term)

所以我想用PHP搜索JSON文件。json链接:。我只是把JSON文件放在我的Web服务器中。这是我的剧本:

<?php
    $query = $_GET['s'];

    $terms =  explode(' ', $query);
    $results = array();

    foreach(file('items.json') as $line) {
        $found = true;

        foreach($terms as $term) {
            if(strpos($line, $term) == false) {
                $found = false;
                break;
            }
        }

        if($found) {
            $results[] = $line;
        } else {

        }
    }
    print_r($results);
您可以使用,和一个闭包(PHP5.3+)来实现这一点

$obj = json_decode(file_get_contents("http://media1.clubpenguin.com/play/en/web_service/game_configs/paper_items.json"), true);

$termStr = "ninja kiwi";
$terms = explode(" ", $termStr);

$results = array_filter($obj, function ($x) use ($terms){
    foreach($terms as $term){
        if (stripos($x["label"], $term) ||
            stripos($x["paper_item_id"], $term))
        {
            return true;
        }
    }
    return false;
});

echo json_encode($results);
您可以使用,和一个闭包(PHP5.3+)来实现这一点

$obj = json_decode(file_get_contents("http://media1.clubpenguin.com/play/en/web_service/game_configs/paper_items.json"), true);

$termStr = "ninja kiwi";
$terms = explode(" ", $termStr);

$results = array_filter($obj, function ($x) use ($terms){
    foreach($terms as $term){
        if (stripos($x["label"], $term) ||
            stripos($x["paper_item_id"], $term))
        {
            return true;
        }
    }
    return false;
});

echo json_encode($results);

[json_decode](可能是您正在查找的内容。请尝试
json_decode(file_get_contents(“items.json”),true);
no,只是一堆错误您打算搜索哪些数据?@Timmy6118CP您可以指定错误吗?警告:file()预期参数1为有效路径,第8行C:\xampp\htdocs\Programs\Test\index.php中给出的数组警告:为第8行C:\xampp\htdocs\Programs\Test\index.php中的foreach()提供的参数无效[json\u decode](可能是您正在查找的内容。Try
json\u decode(file\u get\u contents(“items.json”),true)
no,只是一堆错误您打算搜索哪些数据?@Timmy6118CP您可以指定错误吗?警告:file()希望参数1是有效路径,数组在第8行的C:\xampp\htdocs\Programs\Test\index.php中给出警告:为foreach()提供的参数无效在第8行的C:\xampp\htdocs\Programs\Test\index.php中,谢谢OrangePill!有什么方法可以将print\r更改为echo吗?我这样做了,但我在第8行的C:\xampp\htdocs\Programs\Test\index.php中得到了错误:数组到字符串的转换18@Timmy6118CP您是否正在尝试将json打印出来?如果是,只需更改
print\r($results)
echo json_encode($results)
@Timmy6118CP,真的吗?也许你应该雇个人。@Orangepill我以前试过。只是回显json文件。如果你能展示一个示例,说明你想从这个过程中得到什么,也许会有所帮助。谢谢Orangepill!有没有办法我可以将print\r改为echo?我做了,但我得到了错误:C:\xampp\htdocs中的数组到字符串转换\Programs\Test\index.php联机18@Timmy6118CP您正在尝试将json打印出来吗?如果是,只需将
print_r($results);
更改为
echo json_encode($results)
@Timmy6118CP,真的吗?也许你应该雇个人来。@Orangepill我以前试过。只需回显json文件。如果你能举例说明你希望从这个过程中得到什么,也许会有所帮助