How to search an index by value in two dimensional array in php -


i have function description of courier , records fetched courier , stored in $couriers . $courier 2 dimensional array since containing rows of table couriers .

[ { "id":"1", "name":"dtdc", "description":"automatically inserted application ", "bloked":"false" }, { "id":"2", "name":"ecomm", "description":"nothing", "bloked":"false" }, { "id":"3", "name":"marginprice", "description":"local only", "bloked":"false" } ] 

now , have fetch decription of courier id given . purpose , must know index of records .. tried using array_search "hard time" . , ask give idea know index of records in array

 function getcourierdescriptionbyid($id)     {   global $couriers;         if($couriers==null)         {             loadcourier($id);         }         $index=array_search($id,$couriers);// here problem         return isset($couriers[$index]['description'])?                                             $couriers[$index]['description']:null;           } 

you $courier variable not array , json string , dcode first turns in php array.

$couriers = json_decode($couriers,true); 

now function , can defined below , using array_map

 function getcourierdescriptionbyid($id)     {   global $couriers;         $args=func_num_args();         if($couriers==null)         {             loadcourier($id);         }         $index=array_search($id,array_map("getallcourierid",$couriers));         return isset($couriers[$index]['description'])?                    $couriers[$index]['description']:null;            } 

now function bounded array_map below

 function getallcourierid($arr)         {             return $arr['id'];         } 

Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -