将String转换为数组而不使用php中的explode

时间:2015-01-30 07:04:09

标签: php mongodb codeigniter

我想在mongoDB数据库中将字符串保存为数组。 我得到以下字符串,我需要将其转换为数组,以便在mongoDB数据库中存储数据。
我使用的是codeigniter框架。

输入字符串

"[ [ 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 ] ]"

期望输出

[ 
    [ 
        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
    ]

提前致谢。

1 个答案:

答案 0 :(得分:2)

  

看起来像JSON,为什么不使用JSON解析器呢?

     

它是一个字符串所以它不起作用,我试试。

它确实是一个JSON字符串,与你的主张相反:)

<?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

<强>输出

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
        )

)