1

I want to save string as an array in mongoDB database. I get following string and I need to convert it to array for storing data in mongoDB database.
I am using codeigniter framework.

Input String

"[ [ 18, 18, 18, 18, 18, 18, 18, 18, 18 ], [ 2, 2, 2, 2, 2, 2, 2, 2, 2 ], [ 2, 2, 2, 2, 2, 2, 2, 2, 2 ], [ 2, 2, 2, 2, 2, 2, 2, 2, 2 ], [ 2, 2, 2, 2, 2, 2, 2, 2, 2 ], [ 2, 2, 2, 2, 2, 2, 2, 2, 2 ], [ 2, 2, 2, 2, 2, 2, 2, 2, 2 ], [ 2, 2, 2, 2, 2, 2, 2, 2, 2 ], [ 2, 2, 2, 2, 2, 2, 2, 2, 2 ] ]"

Desired Output

[ 
    [ 
        18, 
        18, 
        18, 
        18, 
        18, 
        18, 
        18, 
        18, 
        18
    ], 
    [ 
        2, 
        2, 
        2, 
        2, 
        2, 
        2, 
        2, 
        2, 
        2
    ], 
    [ 
        2, 
        2, 
        2, 
        2, 
        2, 
        2, 
        2, 
        2, 
        2
    ], 
    [ 
        2, 
        2, 
        2, 
        2, 
        2, 
        2, 
        2, 
        2, 
        2
    ], 
    [ 
        2, 
        2, 
        2, 
        2, 
        2, 
        2, 
        2, 
        2, 
        2
    ], 
    [ 
        2, 
        2, 
        2, 
        2, 
        2, 
        2, 
        2, 
        2, 
        2
    ], 
    [ 
        2, 
        2, 
        2, 
        2, 
        2, 
        2, 
        2, 
        2, 
        2
    ], 
    [ 
        2, 
        2, 
        2, 
        2, 
        2, 
        2, 
        2, 
        2, 
        2
    ], 
    [ 
        2, 
        2, 
        2, 
        2, 
        2, 
        2, 
        2, 
        2, 
        2
    ]

Thanks in advance.

3
  • That looks like JSON, so why not use a JSON parser? Commented Jan 30, 2015 at 7:06
  • It's a string so it's not work, i try that. Commented Jan 30, 2015 at 7:06
  • @Ranpariya ?!?! JSON is a string format. I just tried it in a parser and it worked fine. Commented Jan 30, 2015 at 7:09

1 Answer 1

2

That looks like JSON, so why not use a JSON parser?

It's a string so it's not work, i try that.

It is indeed a JSON string, contrary to your claim :)

<?php

print_r( json_decode("[ [ 18, 18, 18, 18, 18, 18, 18, 18, 18 ], [ 2, 2, 2, 2, 2, 2, 2, 2, 2 ], [ 2, 2, 2, 2, 2, 2, 2, 2, 2 ], [ 2, 2, 2, 2, 2, 2, 2, 2, 2 ], [ 2, 2, 2, 2, 2, 2, 2, 2, 2 ], [ 2, 2, 2, 2, 2, 2, 2, 2, 2 ], [ 2, 2, 2, 2, 2, 2, 2, 2, 2 ], [ 2, 2, 2, 2, 2, 2, 2, 2, 2 ], [ 2, 2, 2, 2, 2, 2, 2, 2, 2 ] ]"));

Fiddle

Output

Array
(
    [0] => Array
        (
            [0] => 18
            [1] => 18
            [2] => 18
            [3] => 18
            [4] => 18
            [5] => 18
            [6] => 18
            [7] => 18
            [8] => 18
        )

    [1] => Array
        (
            [0] => 2
            [1] => 2
            [2] => 2
            [3] => 2
            [4] => 2
            [5] => 2
            [6] => 2
            [7] => 2
            [8] => 2
        )

    [2] => Array
        (
            [0] => 2
            [1] => 2
            [2] => 2
            [3] => 2
            [4] => 2
            [5] => 2
            [6] => 2
            [7] => 2
            [8] => 2
        )

    [3] => Array
        (
            [0] => 2
            [1] => 2
            [2] => 2
            [3] => 2
            [4] => 2
            [5] => 2
            [6] => 2
            [7] => 2
            [8] => 2
        )

    [4] => Array
        (
            [0] => 2
            [1] => 2
            [2] => 2
            [3] => 2
            [4] => 2
            [5] => 2
            [6] => 2
            [7] => 2
            [8] => 2
        )

    [5] => Array
        (
            [0] => 2
            [1] => 2
            [2] => 2
            [3] => 2
            [4] => 2
            [5] => 2
            [6] => 2
            [7] => 2
            [8] => 2
        )

    [6] => Array
        (
            [0] => 2
            [1] => 2
            [2] => 2
            [3] => 2
            [4] => 2
            [5] => 2
            [6] => 2
            [7] => 2
            [8] => 2
        )

    [7] => Array
        (
            [0] => 2
            [1] => 2
            [2] => 2
            [3] => 2
            [4] => 2
            [5] => 2
            [6] => 2
            [7] => 2
            [8] => 2
        )

    [8] => Array
        (
            [0] => 2
            [1] => 2
            [2] => 2
            [3] => 2
            [4] => 2
            [5] => 2
            [6] => 2
            [7] => 2
            [8] => 2
        )

)
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.