test
test

Reputation: 18200

Laravel - Condensing an array

Let's say I have some data from an external API as such:

[
  {
    "a9ff421e-d43a-44e5-8888-e4d3fdefa2b6": false
  },
  {
    "e0625cd1-c5e2-4536-b7d7-762ad5e0e552": 1543968137664
  },
  {
    "6fdcfd1f-b6b0-402e-b10c-01fe1ef508c9": 1543967985149
  }
]

How would I use the Laravel collect() or PHP in a way to make it like this:

{
  "a9ff421e-d43a-44e5-8888-e4d3fdefa2b6": false,
  "e0625cd1-c5e2-4536-b7d7-762ad5e0e552": 1543968137664,
  "6fdcfd1f-b6b0-402e-b10c-01fe1ef508c9": 1543967985149
}

Upvotes: 2

Views: 45

Answers (1)

Josh Young
Josh Young

Reputation: 1366

Laravel has an Array Helper for this.

$collapsed = collect(array_collapse($arrayVar) );

You can read more about it here:

https://laravel.com/docs/5.7/helpers#method-array-collapse

Upvotes: 4

Related Questions