0

In a situation such as this one:

array_walk($first_names, function($item) {
        // do something with the key not the value
    }
);

How can I extract the $item key to use? It's passed as the string value of the $first_names item, but inside the callback function, I would like to use the key not the value. Any ideas?

1 Answer 1

1

Just like its mentioned in the PHP documentation here https://www.php.net/manual/en/function.array-walk.php

Typically, callback takes on two parameters. The array parameter's value being the first, and the key/index second.

So array_walk($first_names, function($item) {

should be array_walk($first_names, function($value, $key) {

Sign up to request clarification or add additional context in comments.

1 Comment

Oops, I had a typo when I tried that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.