2

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"
    }
]
Barmar
  • 741,623
  • 53
  • 500
  • 612
codeDragon
  • 555
  • 1
  • 8
  • 28

2 Answers2

5

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);
ob-ivan
  • 785
  • 1
  • 8
  • 17
  • it´s a good solution. Could you please explain me why just simply writing this does not work? `$jSearchResult = $ajProducts->name === 'A';` – codeDragon Feb 03 '18 at 08:42
  • 1
    Because `$ajProducts` is an array and member dereference (arrow operator) doesn't apply to arrays, only to objects. `name` is a property on an object of your implicit Product type, you need a variable of that type in your expression to be valid. – ob-ivan Feb 03 '18 at 10:54
1

If you want to match elements where the name key value equals "A", then you can just make a check with value of name key is equal to "A" like -

foreach ($ajProducts as $ajProduct ) {

   if ( "A" == $ajProduct->name ) {
     $jSearchResult[] = $ajProduct->name;
     $match_found = true;    
   }
}
Sohel0415
  • 9,523
  • 21
  • 30