user16725381
user16725381

Reputation:

Convert array string into an array Laravel

I've saved some users ids in the database like this:

column user_ids: [2,1]

When I show column's value is "[2,1]"

How can I convert this to an array!

Upvotes: 1

Views: 2861

Answers (2)

Yagnik Sangani
Yagnik Sangani

Reputation: 227

You can try this code :

<?php
$string = "[2,1]";
$result = json_decode($string);
print_r($result);
?>

Output :

Array
(
    [0] => 2
    [1] => 1
)

Upvotes: 1

LF-DevJourney
LF-DevJourney

Reputation: 28564

It's a valid Json string. You can use json_decode to get the array.

json_decode("[2,1]");

Upvotes: 1

Related Questions