0

I'm on laravel. In my controller, i get an array stored in a hidden field. The array comes into a string

'[1,2]'

How i can get the php array from this string? And the array items must be the intenger value.

[1,2]

Of course, have in mind various ways to achieve this, but i want the best. Thanks!

2 Answers 2

3

I think the easiest way in this case would be json_decode():

$string = '[1,2]';
$array = json_decode($string, true);

var_dump($array);

// Output:
array(2) {
  [0] =>
  int(1)
  [1] =>
  int(2)
}
Sign up to request clarification or add additional context in comments.

Comments

0

Use explode() or json_decode() functions

1 Comment

But the "[" and the "]" stay included. For example: "[ 1 , 2 , 3 ]" $newArray = explode (',', $arr); $newArray is [ [1 , 2 , 3] ]

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.