简体   繁体   中英

Converting this (array string ) from a file into php array

I have an export from a legacy system which i want to import into an RDBMS solution.

The export file has data as the example below

    %a = (
'1' => 'test',
'3' => 'test2',
'44' => 'this is another test()
);
    %b = (
'1' => 'super man'

);
    %c = (
'username' => 'testing'

);

I wish to get these files into associative php array so that i can iterate into the database table, check if the values exist and save it if it does not exist.

I am stuck with the parsing of the file part.

so far i have been able to reach here

$string = file_get_contents($path);
        $params = explode("%", $string); // to separate the main chunks

        foreach ($params as $keyvalue) {
            if ($keyvalue!='') { //as the file starts with % to truncate the first blank result
                $trimmed=substr(trim($keyvalue, ' '), 3, strlen($keyvalue) - 4); // remove this part from the result 'a='
                $finalArray = explode(";", $trimmed); // remove the semi column
                $array = $finalArray[0];
                print_r($array );





      echo '<br/>';
      echo '<br/>';
      echo '<br/>';
      echo '<br/>';

      }

with the above i do get this output

( "1" => "test", "3" => "test2" ,'44' => 'this is another test())

( '1' => 'super man'   )

('username' => 'testing' )

displayed on different lines, i tried json_decode (...,true), unserialise ect to convert this output into array so that i can loop with no success.

Any help will be most welcomed.

try this

$array[] = $finalArray[0];

Because you creating new array everytime. You need to append to the existing array so as to get the desired output.

And my edited code is below

$params = explode("%", $string); 
$i = 0;
foreach ($params as $keyvalue) {
 if ($keyvalue!='') {
    $trimmed=substr(trim($keyvalue, ' '), 3, strlen($keyvalue) - 4);
    $finalArray = explode(";", $trimmed);
    $array = $finalArray[0];

    $newArray = explode(',',substr($array, 1, -1));

    foreach($newArray as $arravalue) {
      $newArray1 = explode('=>',substr($arravalue, 1, -1));
      $finalResultArray[$i][trim(str_replace("'","",$newArray1[0]))] = '"'.trim(str_replace("'","",$newArray1[1])).'"';
    }
    $i++;          

  }
}

It gives an output array $finalResultArray with the below format.

Array
(
[0] => Array
    (
        [1] => "test"
        [3] => "test2"
        [44] => "this is another test("
    )

[1] => Array
    (
        [1] => "super man"
    )

[2] => Array
    (
        [username] => "testing"
    )

 )

I made a little tweek with string replace to do the trick, by converting it to a json format replacing the ( by { the => by : and ) by }

it seem to work fine but if there is a neater solution I would like to welcome it.

        $string = file_get_contents($path);
        $params = explode("%", $string);



        foreach ($params as $keyvalue) {
            if ($keyvalue!='') {
                $trimmed=substr(trim($keyvalue, ' '), 3, strlen($keyvalue) - 4);
                $finalArray = explode(";", $trimmed);
                $array = $finalArray[0];        

                 print_r($array); // display 1
                $string2 =  str_replace('(', '{', $array);
            $string3 =  str_replace(')', '}', $string2);
            $string4 =  str_replace('=>', ':', $string3);


            $ar = json_decode($string4);
            echo ('<pre>'); 
                //echo $first;  // value
print_r( $ar);

echo('</pre>'); //display 2


            die('wa');


      echo '<br/>';
      echo '<br/>';
      echo '<br/>';
      echo '<br/>';

      }


            }

//display 1

( "1" => "test1", "2" => "test2", "3" => "test4")

display 2

stdClass Object
(
    [1] => test1
    [2] => test2
    [3] => test4


)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM