Ar Ad Ali Soomro Ar Ad Ali Soomro - 3 months ago 19
PHP Question

Check if a value is present and store in a new array in php

I have this variable with a value

$name=GTX 980


And I have array that has

Arraynames

Array ( [0] => Array ( [name] => GTX 970 [price] => 123 ) [1] => ( [name] => GTX 980 [price] => 321 ))


Now I want to check if array has name that matches to $name and store in a different array I tried this code

for($i=0;i<=count($shophive);$i++){
if(strpos($shophive,$trimmedName ) !== FALSE){
$newshophive[$i]=[
'name' =>$shophive[$i],
'price' =>$shophive[$i],
'link'=>$shophive[$i],
'image'=>$shophive[$i]
];
}
}


But it gives error

Function name must be a string...

Can anyone explain what is wrong and what could be a solution?

Answer

The function below, simply checks to see if given String is contained in a given array. If it does, it adds the element containing that String into a new Array and finally returns the result...

<?php

    $arr = [
        ['name'=>'GTX 970', 'price'=>123],
        ['name'=>'GTX 980', 'price'=>321],
        ['name'=>'GTX 980', 'price'=>350],
        ['name'=>'GTX 980', 'price'=>300],
        ['name'=>'ABC 980', 'price'=>305],
        ['name'=>'JHK 980', 'price'=>375],
        ['name'=>'LMN 105', 'price'=>250],
    ];

    var_dump( searchArrayTree($arr, 'GTX 980') );

    function searchArrayTree($array, $searchValue){
        $result     = [];

        foreach($array as $iKey=>$item){
            $searchKey  = array_search($searchValue, $item);
            if($searchKey){
                $result[] = $item; 
            }

        }
        return $result;
    }

The var_dump() above (which is also demoed here) produces:

    array (size=3)
      0 => 
        array (size=2)
          'name' => string 'GTX 980' (length=7)
          'price' => int 321
      1 => 
        array (size=2)
          'name' => string 'GTX 980' (length=7)
          'price' => int 350
      2 => 
        array (size=2)
          'name' => string 'GTX 980' (length=7)
          'price' => int 300

Alternatively, use regex so that you can narrow your search within group of related words like GTX 980 and still all entries with GTX 980 will be matched as well:

<?php

    $arr = [
        ['name'=>'GTX 970', 'price'=>123],
        ['name'=>'GTX 980', 'price'=>321],
        ['name'=>'GTX 980', 'price'=>350],
        ['name'=>'GTX 980', 'price'=>300],
        ['name'=>'ABC 980', 'price'=>305],
        ['name'=>'JHK 980', 'price'=>375],
        ['name'=>'Le EVGA GTX 980',                 'price'=>250],
        ['name'=>'GIGABYTE GEFORCE GTX 980 W3. ',   'price'=>250],
    ];

    var_dump(searchArrayTree($arr, 'GTX 980'));
    function searchArrayTree($array, $searchTerm){
        $result     = [];

        foreach($array as $iKey=>$item){
            $itemPrice  = $item['price'];
            $itemName   = $item['name'];
            $searchKey  = preg_match("#" . preg_quote($searchTerm). "#", $itemName);
            if($searchKey){
                $result[] = $item;
            }

        }
        return $result;
    }

The result of the var_dump(demoed here) would look like this:

    array (size=5)
      0 => 
        array (size=2)
          'name' => string 'GTX 980' (length=7)
          'price' => int 321
      1 => 
        array (size=2)
          'name' => string 'GTX 980' (length=7)
          'price' => int 350
      2 => 
        array (size=2)
          'name' => string 'GTX 980' (length=7)
          'price' => int 300
      3 => 
        array (size=2)
          'name' => string 'Le EVGA GTX 980' (length=15)
          'price' => int 250
      4 => 
        array (size=2)
          'name' => string 'GIGABYTE GEFORCE GTX 980 W3. ' (length=29)
          'price' => int 250