0

I have a collection that has 2 arrays like on this picture:

enter image description here

So there are:

array 63 has two keys 70 and 72
array 64 has key 71

What i want to do is to get the keys 70, 72, 71.

How to get those keys??

3
  • Have you tried nested foreach? Commented Mar 5, 2017 at 15:45
  • no...can you point me to a direction Commented Mar 5, 2017 at 15:45
  • Sorry I meant nested foreach Commented Mar 5, 2017 at 15:45

2 Answers 2

1
$keys = $collection->flatMap(function ($item) {
    return array_keys($item);
});

If you think there may be duplicates, tack on a call to unique at the end:

$keys = $collection->flatMap(function ($item) {
    return array_keys($item);
})->unique();
Sign up to request clarification or add additional context in comments.

1 Comment

That's what I call "slam & dunk"
1

Try this:

$keys = [];
$collection->each(function ($item) use (&$keys) {
    $keys = array_merge($keys, array_keys($item));
});

Comments

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.