用php搜索无法正常工作

时间:2017-10-14 07:37:24

标签: php json

我正在根据产品名称进行搜索。它实际上工作,它确实根据搜索找到产品,但问题是,当我有几个同名产品时,它只显示我第一个。怎么可以优化?我很感激你的帮助。

<?php

//DATA coming from the browser
$sSearch = $_GET['search'];
//TURN it into UPPERCASE
strtoupper( $sSearch );

//GETTING FROM FILE:
$sajProducts = file_get_contents( 'products.txt' );
$ajProducts = json_decode( $sajProducts );

$matchfound = false;
//LOOPING THROUGH THE ARRAY OF PRODUCTS
for ( $i=0; $i< count( $ajProducts ); $i++ ) {

    if ( $sSearch == $ajProducts[$i]->name ) {
         $jSearchResult = $ajProducts[$i];
         $matchfound = true;    
         break;
    }
}
//if there is a match display the product
if ( $matchfound ) {
    echo json_encode ( $jSearchResult );
    exit;
}
//if not display ALL products
else { 

     echo json_encode( $ajProducts );
     exit;
}

?>

和JSON products.text文件:

[
    {
        "id": "59d278cae7017",
        "name": "A",
        "price": "1",
        "quantity": 3,
        "image": "img_webshop\/productimage-59d74304917c2.jpg"
    },
    {
        "id": "59d27e20c8028",
        "name": "A",
        "price": "2",
        "quantity": 3,
        "image": "img_webshop\/productimage-59d743233c0cf.jpg"
    },
    {
        "id": "59d6a7ae16d15",
        "name": "A",
        "price": "3",
        "quantity": 2,
        "image": "img_webshop\/productimage-59d743392fbb5.jpg"
    },
    {
        "id": "59d6d6ee5f752",
        "name": "A",
        "price": "4",
        "quantity": 4,
        "image": "img_webshop\/productimage-59d74352d5b94.jpg"
    },
    {
        "id": "59d743d207bd5",
        "name": "B",
        "price": "5",
        "quantity": 1,
        "image": "img_webshop\/productimage-59d743d1e6e64.jpg"
    },
    {
        "id": "59d74451225ac",
        "name": "B",
        "price": "6",
        "quantity": 1,
        "image": "img_webshop\/productimage-59d7445120871.jpg"
    },
    {
        "id": "59e0d95cd4111",
        "name": "B",
        "price": "4",
        "quantity": "5",
        "image": "img_webshop\/productimage-59e0d95cd2c4b.jpg"
    },
    {
        "id": "59e0d992d1f3b",
        "name": "C",
        "price": "6",
        "quantity": "5",
        "image": "img_webshop\/productimage-59e0d992d19be.jpg"
    },
    {
        "id": "59e0d9c59fbf2",
        "name": "D",
        "price": "4",
        "quantity": "5",
        "image": "img_webshop\/productimage-59e0d9c59f1a5.jpg"
    }
]

2 个答案:

答案 0 :(得分:0)

使用此代码:

&#13;
&#13;
$matchfound = false;
$jSearchResult = array();
//LOOPING THROUGH THE ARRAY OF PRODUCTS
for ( $i=0; $i< count( $ajProducts ); $i++ ) {

    if ( $sSearch == $ajProducts[$i]->name ) {
         $jSearchResult[] = $ajProducts[$i];
         $matchfound = true;   
    }
}
&#13;
&#13;
&#13; 而不是:

&#13;
&#13;
$matchfound = false;
//LOOPING THROUGH THE ARRAY OF PRODUCTS
for ( $i=0; $i< count( $ajProducts ); $i++ ) {

    if ( $sSearch == $ajProducts[$i]->name ) {
         $jSearchResult = $ajProducts[$i];
         $matchfound = true;    
         break;
    }
}
&#13;
&#13;
&#13;

答案 1 :(得分:0)

在数组中收集所有匹配的结果,然后以JSON格式返回。

$jSearchResult = array();
for ( $i=0; $i< count( $ajProducts ); $i++ ) {

    if ( $sSearch == $ajProducts[$i]->name ) {
         $jSearchResult[] = $ajProducts[$i];
         $matchfound = true;    
         //break;
    }
}

所以,在foreach循环完成所有json值的迭代之后,我们将在$jSearchResult

中包含所有匹配的最终数组

然后返回:

if ( $matchfound ) {
    echo json_encode ( $jSearchResult );
    exit;
}