Communities for your favorite technologies. Explore all Collectives
Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work.
Bring the best of human thought and AI automation together at your work. Learn more
Find centralized, trusted content and collaborate around the technologies you use most.
Stack Internal
Knowledge at work
Bring the best of human thought and AI automation together at your work.
Is it possible to get the array key name this way?
$array = ("first" => 1); function f($object) { echo(???); //should give first } f($array['first']);
It's not possible if you pass only the value into the function like f($object['first']), the key name has no relation to the passed value in that case.
f($object['first'])
You need to pass the entire array (f($array)) and use:
f($array)
echo key($object);
Add a comment
I don't really know what you want to get, but I guess this will help you:
$array = array('foo' => 'bar', 'baz' => 'foobar'); foreach ($array as $key => $value) { echo $key . ' = ' . $value . '<br />'; }
This will return
foo = bar baz = foobar
I would use
$array = ("first" => 1); function f($object, $key) { echo($key); // will give first echo($object[$key]); // will give 1 } f($array, 'first');
Required, but never shown
By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.