Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
176 views
in Technique[技术] by (71.8m points)

regex - String to Multi-Dimensional array in PHP

Imagine that I have a variable that contains following value:

$content = "[['a', 'b', 1, 4], ['a2', 'b2', 12, 42], ['a3', 'b3', 13, 43], ['a4', 'b4', 14, 44]]";

I want to parse this string and create actual multi-dimensional array in PHP. For example:

Array
(
    [0] => Array
        (
            [0] => 'a'
            [1] => 'b'
            [2] => 1
            [3] => 4
        )

    [1] => Array
        (
            [0] => 'a2'
            [1] => 'b2'
            [2] => 12
            [3] => 42
        )
    [2] => Array
        (
            [0] => 'a3'
            [1] => 'b3'
            [2] => 13
            [3] => 43
        )

    [3] => Array
        (
            [0] => 'a4'
            [1] => 'b4'
            [2] => 14
            [3] => 44
        )
)

For that purpose, first of all I tried to parse that string via regular expression:

$pattern = "/[([.+])]/i";

However I failed when I tried it as following:

$pattern = "/[([.+])]/i";
$content = "[['a', 'b', 1, 4], ['a2', 'b2', 12, 42], ['a3', 'b3', 13, 43], ['a4', 'b4', 14, 44]]";

preg_match_all($pattern, $content, $results);

print_r($results);

And the output is:

Array ( 
    [0] => Array ( [0] => [['a', 'b', 1, 4], ['a2', 'b2', 12, 42], ['a3', 'b3', 13, 43], ['a4', 'b4', 14, 44]] ) 
    [1] => Array ( [0] => ['a', 'b', 1, 4], ['a2', 'b2', 12, 42], ['a3', 'b3', 13, 43], ['a4', 'b4', 14, 44] ) 
) 

So;

  1. How can I solve that regex issue?
  2. Are there any other ways to implement that problem?

Thanks.

question from:https://stackoverflow.com/questions/65646610/string-to-multi-dimensional-array-in-php

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Convert it to JSON, which is simple all you need to do is replace the single quotes with double quotes, then it will be a JSON String

$content = "[['a', 'b', 1, 4], ['a2', 'b2', 12, 42], ['a3', 'b3', 13, 43], ['a4', 'b4', 14, 44]]";
$c = str_replace("'", '"', $content);

print_r(json_decode($c));

Result

Array
(
    [0] => Array
        (
            [0] => a
            [1] => b
            [2] => 1
            [3] => 4
        )

    [1] => Array
        (
            [0] => a2
            [1] => b2
            [2] => 12
            [3] => 42
        )

    [2] => Array
        (
            [0] => a3
            [1] => b3
            [2] => 13
            [3] => 43
        )

    [3] => Array
        (
            [0] => a4
            [1] => b4
            [2] => 14
            [3] => 44
        )

)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...