如何在PHP中将这个简单的文本块解析为多维数组?

6
我有一个简单的文本块,内容如下:
    txt_1(val_1,val_2,val_3).
    txt_1(val_4,val_5,val_6).
    txt_2(val_7,val_8,val_9).
    txt_3(val_10,val_11,val_12).
    txt_3(val_13,val_14,val_15).
    txt_4(val_16,val_17,val_18).
PHP代码中已经有一个简单的数组:
$my_array = array();

现在,我希望将它解析为 PHP 数组,如下所示:
Array
(
    [txt_1] => Array
        (
            [0] => Array
                (
                    [0] => val_1
                    [1] => val_2
                    [2] => val_3
                )

            [1] => Array
                (
                    [0] => val_4
                    [1] => val_5
                    [2] => val_6
                )

        )

    [txt_2] => Array
        (
            [0] => Array
                (
                    [0] => val_7
                    [1] => val_8
                    [2] => val_9
                )

        )

    [txt_3] => Array
        (
            [0] => Array
                (
                    [0] => val_10
                    [1] => val_11
                    [2] => val_12
                )

            [1] => Array
                (
                    [0] => val_13
                    [1] => val_14
                    [2] => val_15
                )

        )

    [txt_4] => Array
        (
            [0] => Array
                (
                    [0] => val_16
                    [1] => val_17
                    [2] => val_18
                )

        )

)

所有的数据都是通用的。你能帮我用PHP来实现吗?
4个回答

2
<?php

// Initially put your input into a variable
$txt=<<<__EOT__
txt_1(val_1,val_2,val_3).
txt_2(val_4,val_5,val_6).
txt_n(val_a,val_b,val_c).
__EOT__;

$result = array();

// separate out each row
$rows = explode("\n", $txt);

    // loop through each row
    foreach($rows as $row) {

    // Use a regular expression to find the key and values
    $success = preg_match('/^([^(]+)\(([^)]+)\)\.$/', $row, $parts);

    // Check the regexp worked
    if(!$success) {
        echo 'Failed to match row: ' . $row . "\n";
        continue;
    }

    // get the array key from the regexp results
    $key = $parts[1];

    // the values are all a string, split on the comma to make an array
    $values = explode(',', $parts[2]);

    // store $key and $values in the result
    $result[$key] = $values;

}

// See if it worked
var_dump($result);

你测试过答案了吗? - Steve E.

2
    Suppose this answer will help you 

    $text = " 
    txt_1(val_1,val_2,val_3).
    txt_2(val_4,val_5,val_6).
    txt_3(val_a,val_b,val_c).
    ";

    $myArry = explode(".", $text);
    $resArry = array();

    foreach ($myArry as $key => $value) {
        if(trim($value)!=""){
            $plain = str_replace(array("(",")"),",",$value);
            $subArry = explode(",",$plain);

            $keyN = explode("(",trim($value));
            unset($subArry[array_search($keyN[0],$subArry)]);
            unset($subArry[array_search("",$subArry)]);
            $resArry[$keyN[0]][]=$subArry;    
        }

    }

    echo "<pre/>";
    print_r($resArry);
    die;

    //Output will be like 
Array
(
    [txt_1] => Array
     (
        [0] => Array
            (
                [1] => val_1
                [2] => val_2
                [3] => val_3
            )

    )

   [txt_2] => Array
     (
        [0] => Array
            (
                [1] => val_4
                [2] => val_5
                [3] => val_6
            )

    )

 [txt_3] => Array
     (
        [0] => Array
            (
                [1] => val_a
                [2] => val_b
                [3] => val_c
            )

    )

)

@Viral,我认为这个答案也适用于动态数组,例如txt_1(val_1,val_2,val_3)。 txt_1(val_4,val_5,val_6)。 txt_2(val_7,val_8,val_9)。 txt_3(val_10,val_11,val_12)。 txt_3(val_13,val_14,val_15)。 txt_4(val_16,val_17,val_18)。 - D M Patel

1

在这里测试:http://phptester.net/

注意:为了使用短数组语法[],需要使用PHP 5.4版本。

<?php

$text = " 
        txt_1(val_1,val_2,val_3).
        txt_2(val_4,val_5,val_6).
        txt_n(val_a,val_b,val_c).
        ";

$myArray = [];

//You're gonna see why we want to remove this character
//later, it will help us have a cleaner code.
$text = str_replace(')', '', $text);

$arrayGroup = explode('.', $text);

//print_r($arrayGroup);

foreach($arrayGroup as $array) {
    $exp = explode('(', $array);    
    $arrayName = trim($exp[0]);
    $arrayValues = explode(',', $exp[1]);

    foreach($arrayValues as $value) {
        ${$arrayName}[] = $value;
    }

    $myArray[$arrayName] = $$arrayName;
}

echo '<pre>';
print_r($myArray);
echo '</pre>';

echo '<pre>';
print_r($myArray['txt_2']);
echo '</pre>';

在这之后,您可以使用 txt_1 或 txt_2 或其他变量,因为这些变量是动态创建的。
在代码的后面,您可以毫无问题地使用 $myVar = txt_1[3];

1
$input = 'txt_1(val_1,val_2,val_3).
        txt_1(val_4,val_5,val_6).
        txt_2(val_7,val_8,val_9).
        txt_3(val_10,val_11,val_12).
        txt_3(val_13,val_14,val_15).
        txt_4(val_16,val_17,val_18).'; // the input string

$temp = explode('.', $input); // seprates from .
$temp = array_filter($temp); // for cutting blank values
$temp = array_map('trim', $temp); // removes newlines

$final = [];

foreach($temp as $val)
{
    $key = strtok($val, '('); // search upto token (
    $final[$key][] = explode(',' ,strtok(')')); // advance token to )
}

unset($val, $temp); // unset non required things

这是关于变量$final的输出:
Array
(
    [txt_1] => Array
        (
            [0] => Array
                (
                    [0] => val_1
                    [1] => val_2
                    [2] => val_3
                )

            [1] => Array
                (
                    [0] => val_4
                    [1] => val_5
                    [2] => val_6
                )

        )

    [txt_2] => Array
        (
            [0] => Array
                (
                    [0] => val_7
                    [1] => val_8
                    [2] => val_9
                )

        )

    [txt_3] => Array
        (
            [0] => Array
                (
                    [0] => val_10
                    [1] => val_11
                    [2] => val_12
                )

            [1] => Array
                (
                    [0] => val_13
                    [1] => val_14
                    [2] => val_15
                )

        )

    [txt_4] => Array
        (
            [0] => Array
                (
                    [0] => val_16
                    [1] => val_17
                    [2] => val_18
                )

        )

)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接