1

I want to convert to this array

$string = "1,[1,2,3],[2,2,4],2,3"; To Example

0 => 1,
1 => [
       0 => 1,
       1 => 2,
       2 => 3
     ],
2 => [
       0 => 2,
       1 => 2,
       2 => 4
     ],
3 => 2,
4 => 3
3
  • 1
    Try like this dd(json_encode($yourArray)); Commented Oct 29, 2020 at 5:26
  • 1
    json_decode("[{$string}]") should do it (or json_decode("[$string]") if you want the shortest version). Commented Oct 29, 2020 at 5:28
  • Is this your way of serialization? Commented Oct 29, 2020 at 5:28

1 Answer 1

5

You can use json_decode() :

$string = "1,[1,2,3],[2,2,4],2,3";
$array = json_decode("[$string]", true);
print_r($array);

Output :

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

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

    [3] => 2
    [4] => 3
)

Above code tested here

Sign up to request clarification or add additional context in comments.

Comments

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.