Mohammad Hosseini
Mohammad Hosseini

Reputation: 1797

how to get one object of array in laravel

I use tagify plugin. In my Laravel application controller I try to get tags:

code:

$tags = $request->tags;

result: (returned as string)

[ { value: "css" }, { value: "dfs" } ]

I tried $tags = json_decode($tags); to convert to array. This returns below result:

[
  {
    value: "css"
  },
  {
    value: "dfs"
  }
]

I just need css and dfs (no need value: and double quotes). How to get these?

Upvotes: 0

Views: 273

Answers (1)

Salim Djerbouh
Salim Djerbouh

Reputation: 11034

Here's what you can do using Laravel's collections

$json = '[ { "value": "css" }, { "value": "dfs" } ]';
$json = json_decode($json);
return collect($json)->pluck('value')->toArray();

Results:

[
  "css",
  "dfs"
]

Upvotes: 1

Related Questions