Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to parse this simple text-block into a multi-dimensional array, in PHP?

I have a simple text-block; and, its content is:

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

And, there is already an simple array, in PHP code:

$my_array = array();

Now, I want to parse into this PHP array, like:

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
                )

        )

)

All data is general. Could you help me to do it, with PHP?

like image 884
5352580 Avatar asked Sep 19 '15 05:09

5352580


3 Answers

<?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);
like image 125
Steve E. Avatar answered Nov 01 '22 13:11

Steve E.


    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
            )

    )

)
like image 2
D M Patel Avatar answered Nov 01 '22 13:11

D M Patel


$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

Here is the output for $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
                )

        )

)
like image 1
viral Avatar answered Nov 01 '22 13:11

viral