1

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.

0

2 Answers 2

1

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

 )
Sign up to request clarification or add additional context in comments.

4 Comments

Hello thank you for the suggestion. The real issue is that with the output of this string format ( '1' => 'super man' ) i am not being able to convert it into an associative php array so that i can say like $test['1] to get super man or at least $test[1] = super man ( normal array) so that i can make a loop with it and read all the data
this one works perfectly too, thanks, was stuck with it now i got 2 solutions :) thumb up
just a little issue i noticed is that we have 2 times "" for example [0] => Array ( ["33"] => ""testx" ["32"] => ""testy"
I have given the solution for the example string format which you gave us.
0

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


)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.