将字符串转换为可用的json字符串或数组对象

时间:2014-05-12 14:31:06

标签: php arrays json

一个函数将两个参数作为func($t,$stringt){}

其中$ t是整数且$stringt是任何字符串,$stringt的格式如下

for $t = 1;
$stringt = '({A,B,C,D,E,F},{(A,B),(B,C),(C,A),(E,D),(D,A),(F,A)})'; 
//or it can be
$stringt = '({A,B,C,D,E,F},{(A,B),(B,C),(C,A),(D,A),(B,A),(A,E),(F,A)})';
for $t = 2;
    $stringt = '({A,B,C},{(A,B),(B,C),(C,A)}),({A,B,C,D,E},{(A,B),(B,C),(C,A),(D,A),(E,D),(A,E),(E,C),(D,B)})';

如何将此简单字符串转换为任何可用的json字符串

这样说

 $json = '[

                        [
                             [
                                "A", "B", "C"
                             ],
                             [
                                [
                                    "A" , "B"
                                ],
                                [
                                    "B","C"
                                ],
                                [
                                    "C","A"
                                ]
                             ]
                        ],
                        [
                             [
                                "A", "B", "C", "D", "E"
                             ],
                             [
                                [
                                    "A","B"
                                ],
                                [
                                    "B","C"
                                ],
                                [
                                    "C","A"
                                ],
                                [
                                    "D","A"
                                ],
                                [
                                    "E","D"
                                ],
                                [
                                    "A","E"
                                ],
                                [
                                    "E","C"
                                ],
                                [
                                    "D","B"
                                ]
                             ]

                        ] 
        ]';

有了这个,我可以接近所需的结果。 我看的输出类似于

所有与第二组元素A的组合

$array_a = array( array('A','B'),array('C','A'),array('D','A'),array('A','E'));
var_dump($array_a);
echo count($array_a);

同样适用于所有价值对。

是否有任何其他方法可以接近给定的输入字符串,比如在第一组中找到A(或任何其他)的所有值pare,然后在第二组和任何第n组中查找。

1 个答案:

答案 0 :(得分:1)

如果您确定格式,这对我有用:

$json = '({A,B,C,D,E,F},{(A,B),(B,C),(C,A),(E,D),(D,A),(F,A)})';

$json = preg_replace('/([^\(\)\{\}\[\],]+)/', '"$1"', $json);
$json = preg_replace('/[\{\(]/', '[', $json);
$json = preg_replace('/[\}\)]/', ']', $json);

$array = json_decode($json, true);

echo '<pre>';

print_r($array);

结果:

Array
(
    [0] => Array
        (
            [0] => A
            [1] => B
            [2] => C
            [3] => D
            [4] => E
            [5] => F
        )

    [1] => Array
        (
            [0] => Array
                (
                    [0] => A
                    [1] => B
                )

            [1] => Array
                (
                    [0] => B
                    [1] => C
                )

            [2] => Array
                (
                    [0] => C
                    [1] => A
                )

            [3] => Array
                (
                    [0] => E
                    [1] => D
                )

            [4] => Array
                (
                    [0] => D
                    [1] => A
                )

            [5] => Array
                (
                    [0] => F
                    [1] => A
                )

        )

)

但是,它可能不适用于包含[] {} () ,任何内容的项目。