猿问

PHP - 如何查询 JSON 文件的多个值?

我正在使用纯 PHP 和 JS 构建 JSON 后端,不涉及 MySQKL,我需要知道是否可以多次查询相同的 JSON 键模式。


所以,这是我的 JSON 文件:


[

    {

        "id": "WWJaePyv9a",

        "string": "aaa",

        "number": 123.89,

        "aArray": [

            "aa",

            "bb",

            "cc"

        ],

        "bool": true,

        "geocoords": [

            "1.23456",

            "-33.76584"

        ]

    },

    {

        "id": "vXMv68ze8K",

        "string": "bbb",

        "number": 1.64,

        "aArray": [

            "yyy",

            "ppp"

        ],

        "bool": false,

        "geocoords": [

            ""

        ]

    },

    {

        "id": "Imd6oPVYtP",

        "string": "ccc",

        "number": 0,

        "aArray": [

            ""

        ],

        "bool": false,

        "file": "",

        "geocoords": [

            ""

        ]

    }

]

这是我的 PHP 脚本:


<?php 

$className = basename(__DIR__) ;


/* GET variables */

$string = $_GET['string'];

$aArray = $_GET['aArray'];

$number = $_GET['number'];

$bool = $_GET['bool'];

if( $bool == 'true'){ $bool = true; } else if($bool == 'false'){ $bool= false; }



$data = file_get_contents($className. '.json');

$data_array = json_decode($data, true);


$results = array();


foreach ($data_array as $obj) {


    // check value in string

    if(isset($string)){

        if (strpos($obj['string'], $string) !== false) {

            // $results[$obj['id']] = $obj;

            array_push($results, $obj);

        }

    }


这可能吗?我是在代码中做错了什么,还是在查询 URL 的语法中做错了什么?


慕码人2483693
浏览 152回答 1
1回答

绝地无双

我可以使用以下代码解决问题:if(isset($string)){&nbsp; &nbsp; &nbsp; &nbsp; foreach ($string as $k=>$value) {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ($obj['string'] == $value) {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; array_push($results, $obj);&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; if(isset($number)){&nbsp; &nbsp; &nbsp; &nbsp; foreach ($number as $k=>$value) {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ($obj['number'] == $value) {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; array_push($results, $obj);&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; if(isset($aArray)){&nbsp; &nbsp; &nbsp; &nbsp; foreach ($aArray as $k=>$value) {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (in_array($value, $obj['aArray'])) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; array_push($results, $obj);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; if(isset($bool)){&nbsp; &nbsp; &nbsp; &nbsp; foreach ($bool as $k=>$value) {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ($obj['bool'] == $value) {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; array_push($results, $obj);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }因此,使用这样的 URL:https://example.com/query.php?string[]=aaa&string[]=ccc&aArray[]=ppp使我能够得到我正在寻找的结果。
随时随地看视频慕课网APP
我要回答