3

Im using Laravel 4.2. This maybe quite simple. When i get result from eloquent model using ->get(), i get this type of result

[{"name":"JOHN","tel":"12345"},{"name":"JANE","tel":"67890"}]

And I want to convert to this format

[["JOHN","12345"],["JANE","67890"]]

I've been struggling with this issue and I don't know what exact keyword to search for.

2 Answers 2

5

Use the map() collection method and array_values(). Just tested this and it's working perfectly:

$collection->map(function ($i) {
    return array_values($i);
})->toArray();

In 4.2 use array_map:

array_map(function ($i) {
    return array_values($i);
}, $collection->toArray());
Sign up to request clarification or add additional context in comments.

2 Comments

this is laravel 4.2. map() is not available.
@lucaditrimma I've updated the answer with a solution for 4.2 too.
3

https://laravel.com/docs/4.2/eloquent#collections

This may be a shorter way to do as each method is availble on collections too, Although @Alexey Answer is nice one.

$collection->each(function ($i) {
    return array_values($i);
})->toArray();

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.