phparraysmultidimensional-arrayfilter

Search a 2d array for a qualifying column value and return its first level key


I want to make a search function in the following array:

array(      
    array("key" => "net_sale",         "value" => "NET SALE CRTN"),
    array("key" => "productive_calls", "value" => "Productive Calls"),
    array("key" => "drop_size_value",  "value" => "DROP SIZE VALUE"),
    array("key" => "sku",              "value" => "SKU PER BILL"),
    array("key" => "net_amount",       "value" => "Net Amount"),
    array("key" => "drop_size_crtn",   "value" => "DROP SIZE CRTN"),
    array("key" => "productive_pops",  "value" => "PRODUCTIVE POPS"),
    array("key" => "scheduled_pops",   "value" => "SCHEDULED POPS") 
);

This function will return the position for any key searched. (for example for search "net_amount" it will return 4)


Solution

  • function search($array,$key_value) {
       foreach($array AS $key => $value) {
         if($value['key'] == $key_value) return $key;
       }
    }
    echo search($arr,'net_amount'); //$arr contains the array you gave as example