John Davidson

php - Convert array string into an array Laravel

0 comments
Message:


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!



My Answer:
You can use the `json_decode` function in Laravel to convert the string representation of an array into an actual array. Here's an example of how you can do this:

php
$userIds = '[2,1]';
$userIdsArray = json_decode($userIds);

dd($userIdsArray);


This will output:


array:2 [
0 => 2
1 => 1
]


Now you have the user ids as an actual array that you can work with in your Laravel application.

Rate this post

5 of 5 based on 8382 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us