【问题标题】:PHP - make query to JSON file with AND conditionPHP - 使用 AND 条件查询 JSON 文件
【发布时间】:2023-03-06 21:04:01
【问题描述】:

我正在尝试使用 PHP 从 JSON 文件中查询一些数据,我想获取基于 stringnumber 的对象。

这是我的 JSON:

[
    {
        "id": "5V28tqJ1",
        "string": "john doe",
        "number": 1,
        "aArray": [
            "cc",
            "kk",
            "lo",
            "mm"
        ],
        "bool": false,
        "createdAt": "2020-01-02T15:16:11",
        "updatedAt": "2020-01-05T19:37:02"
    },
    {
        "id": "PMuKM818",
        "string": "sarah doe",
        "number": 2,
        "aArray": [
            "bb",
            "cc"
        ],
        "bool": true,
        "createdAt": "2020-01-02T16:16:23",
        "updatedAt": "2020-01-05T19:37:14"
    },
    {
        "id": "m8HSbEQe",
        "string": "bob smith",
        "number": 3,
        "aArray": [
            "dd",
            "ee",
            "ff"
        ],
        "bool": false,
        "createdAt": "2020-01-02T17:16:36",
        "updatedAt": "2020-01-05T19:37:32"
    }
]

这是我的query-user.php 脚本:

<?php include 'header.php';
$string = $_GET['string'];
$aArray = $_GET['aArray'];
$number = $_GET['number'];
$bool = $_GET['bool'];

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

$results = array();

for ($i=0; $i<count($data); $i++) {

    // check value in string
    if(isset($string)){
        if (strpos($data[$i]->string, $string) !== false) {
            array_push($results, $data[$i]);
        }
    }

    // check element in array
    if(isset($aArray)){
        if (in_array($aArray, $data[$i]->aArray)) {
            array_push($results, $data[$i]);
        } 
    }

    // check value in number
    if(isset($number)){
        if (strpos($data[$i]->number, $number) !== false) {
            array_push($results, $data[$i]);
        } 
    }

    // check value in bool
    if(isset($bool)){
        if ($data[$i]->bool == $bool) {
            array_push($results, $data[$i]);
        }
    }

}// ./ for

echo json_encode($results, JSON_PRETTY_PRINT);
?>

在我的浏览器上,我调用以下网址:

http://example.com/users/query-user.php?string=doe&number=2

这是我得到的结果:

[ 
{ "id": "5V28tqJ1", "string": "john doe", "number": 1, "aArray": [ "cc", "kk", "lo", "mm" ], "bool": false, "createdAt": "2020-01-02T15:16:11", "updatedAt": "2020-01-05T19:37:02" }, 
{ "id": "PMuKM818", "string": "sarah doe", "number": 2, "aArray": [ "bb", "cc" ], "bool": true, "createdAt": "2020-01-02T16:16:23", "updatedAt": "2020-01-05T19:37:14" },
{ "id": "PMuKM818", "string": "sarah doe", "number": 2, "aArray": [ "bb", "cc" ], "bool": true, "createdAt": "2020-01-02T16:16:23", "updatedAt": "2020-01-05T19:37:14" }
 ]

如你所见,我得到了 ID 为 PMuKM818 的对象两次,而我应该得到一次,连同第一个对象。

我在我的 php 代码中做错了什么?

【问题讨论】:

    标签: php arrays json string


    【解决方案1】:

    ID 为 PMuKM818 的对象对应于您的两个查询参数:它包含 Doe 并且编号为 2。

    如果您想防止这种情况发生,请使用 if else 语句而不是简单的 if 语句为您的过滤器设置优先顺序,或者在添加元素之前在结果数组中添加对现有 ID 的检查。

    附加编辑

    不是最优雅的解决方案,但它可以满足您的需求:

    foreach ($data as $details) {
    
        // check value in string
        if(isset($string)){
            if (strpos($details->string, $string) !== false) {
                $results[$details->id] = $details;
            }
        }
    
        // check element in array
        if(isset($aArray)){
            if (in_array($aArray, $details->aArray)) {
                $results[$details->id] = $details;
            } 
        }
    
        // check value in number
        if(isset($number)){
            if (strpos($details->number, $number) !== false) {
                $results[$details->id] = $details;
            } 
        }
    
        // check value in bool
        if(isset($bool)){
            if ($details->bool == $bool) {
                $results[$details->id] = $details;
            }
        }
    
    }// ./ for
    
    echo json_encode(array_values($results), JSON_PRETTY_PRINT);
    

    我还用foreach 替换了for 循环,但这不会影响结果

    【讨论】:

    • 我尝试过使用 else if,但它是错误的。您可以使用示例代码编辑您的问题吗?谢谢
    • 感谢分享您的代码,无论如何,如果我使用“example.com/users/query-user.php?number=3&string=john”,我只会得到第一个对象,而不是第三个对象。这就是我说我已经尝试过的原因else if 语句,就像你做的那样,但结果不是我需要的
    • 谢谢,现在它可以满足我的需要!
    【解决方案2】:

    如需快速解答,您只需使用:https://www.php.net/manual/en/function.array-column

    处理 json 对象的最快方法是什么。

    更新:

    如需完整代码正确,请使用:https://www.php.net/manual/en/function.array-search

    结合array_column,用什么方式搜索会更快,然后逐个搜索。

    【讨论】:

    • 谢谢,您可以发布与我的问题相关的示例代码吗?这对我很有帮助,我是新手 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-01-05
    • 2013-01-11
    • 1970-01-01
    • 2020-10-17
    • 1970-01-01
    • 2016-12-29
    • 1970-01-01
    相关资源
    最近更新 更多