Search code examples
phpkeyvaluepair

Search elements of an array by key value


I have the following PHP code which searches the array by the key name and returns result equals to the search. Just for curiosity how can I do that, that no matter what I search for the result will be elements where the name key value equals "A"? Shortly how can I retrieve elements where name is "A" ?

I tried the following but does´t work:

$jSearchResult[] = $ajProducts[$i]->name = "A";

Please help me cause I just simply can´t figure out how to retrive elements of an array by key value, however I am sure it must be something very simple.

<?php

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

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

$match_found = false;
//Collect all matching result in an array 
$jSearchResult = array();
//LOOPING THROUGH THE ARRAY OF PRODUCTS
for ( $i=0; $i< count( $ajProducts ); $i++ ) {

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

     echo json_encode ( $ajProducts );
     exit;
}

?>

$ajProducts looks like this:

[
    {
        "id": "59d278cae7017",
        "name": "A",
        "price": "1",
        "quantity": 2,
        "image": "img_webshop\/productimage-59d74304917c2.jpg"
    },
    {
        "id": "59d27e20c8028",
        "name": "A",
        "price": "2",
        "quantity": 1,
        "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": 1,
        "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": 0,
        "image": "img_webshop\/productimage-59d7445120871.jpg"
    },
    {
        "id": "59e0d992d1f3b",
        "name": "C",
        "price": "6",
        "quantity": 2,
        "image": "img_webshop\/productimage-59e725ac79583.jpg"
    }
]

Solution

  • There is a php function that does just that: http://php.net/array_filter

    $searchResult = array_filter($ajProducts, function ($product) {
        return $product->name === 'A';
    });
    

    This will get you all objects in $ajProducts having name property set to 'A'.

    To see if there're any results:

    $matchFound = !empty($searchResult);