【发布时间】:2013-08-16 22:00:57
【问题描述】:
所以我想用 PHP 搜索一个 JSON 文件。 json 链接:http://media1.clubpenguin.com/play/en/web_service/game_configs/paper_items.json。我只是将 JSON 文件放在我的网络服务器中。这是我的脚本:
<?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);
问题是它显示了整个 json 文件而不是我的 $query。我能做些什么来解决这个问题?
【问题讨论】:
-
[json_decode](php.net/json_decode] 可能是你要找的。试试
json_decode(file_get_contents("items.json"), true); -
不,只是一堆错误
-
您打算搜索哪些数据?
-
@Timmy6118CP 你能指出错误吗?
-
警告:file() 期望参数 1 是有效路径,数组在第 8 行的 C:\xampp\htdocs\Programs\Test\index.php 中给出警告:为 foreach( ) 在第 8 行的 C:\xampp\htdocs\Programs\Test\index.php 中