OiO.lk Blog PHP Can I carry the key of an array element when passing it as argument to a function?
PHP

Can I carry the key of an array element when passing it as argument to a function?


Given this array:

$food = [
          'meat' => ['beef','chicken'],
          'veg' => ['salad','onions'],
           ]

and this function

function foo($a) {
   print key($a);
}

invoking foo($food['meat']) returns 0 instead meat

Since I need to know which element I’m working on I can add another argument to the function, but I wonder if there’s is a way to access the original key.

Thanks in advance.



You need to sign in to view this answers

Exit mobile version