October 22, 2024
Chicago 12, Melborne City, USA
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

Leave feedback about this

  • Quality
  • Price
  • Service

PROS

+
Add Field

CONS

+
Add Field
Choose Image
Choose Video