PHP搜索JSON文件

时间:2013-11-14 01:21:39

标签: php json

到目前为止,我有这个:

<?php
$obj = json_decode(file_get_contents("URLTOJSONFILEHERE"), true);

$termStr = "WORD TO SEARCHHERE";
$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 print_r($results);

它做得对,但是如果没有print_r和json_encode怎么办?有没有办法把它放在一个html表中? 结果我得到了:

{"103":{"paper_item_id":215,"type":5,"cost":320,"is_member":true,"label":"Life Jacket","prompt":"Life Jacket","layer":3000},"120":{"paper_item_id":232,"type":5,"cost":700,"is_member":true,"label":"Green Suede Jacket","prompt":"Green Suede Jacket","layer":3000},"125":{"paper_item_id":237,"type":5,"cost":700,"is_member":true,"label":"Red Suede Jacket","prompt":"Red Suede Jacket","layer":3000},"126":{"paper_item_id":238,"type":5,"cost":700,"is_member":true,"label":"Pastel Suede Jacket","prompt":"Pastel Suede Jacket","layer":3000},"129":{"paper_item_id":241,"type":5,"cost":700,"is_member":true,"label":"Purple Suede Jacket","prompt":"Purple Suede Jacket","layer":3000}

JSON的第一个数字是什么意思:103,120等 我怎么能不以JSON文件形式显示它?

1 个答案:

答案 0 :(得分:0)

您正在获取JSON编码数组。 103,120是索引引用或数组键。一旦使用了json_decode,就可以遍历生成的数组并输出内容。 HEre是一些伪代码:

$obj = json_decode("...");
foreach($obj as $key=>$value){

  echo $value['paper_item_id'];
  echo $value['type'];
  etc.......

}

当然,您还需要在此处添加HTML标记。