0

I have this file that I would like to read into a multidimenstional array in php: file. If I take the first set of lines as a first example, I would like the print_r to look something like this:

Array
(
    [SiiNunit] => Array
        (
            [0] => Array
                (
                    [economy] => _nameless.2BB8.4FB8
                    [economy_data] => Array
                        [0] => Array
                            (
                                [bank] = _nameless.2917.43B0
                                [player] = _nameless.2813.6928
                                [companies] = 312
                                [companies[0]] = company.volatile.euroacres.nurnberg
                                [companies[1]] = company.volatile.euroacres.erfurt
                                etc...

Then as another example, further down the file when it is listing all of the jobs like this:

job_offer_data : _nameless.2BD0.8940 {
 cargo: null
 company_truck: ""
 variant: nil
 target: ""
 expiration_time: nil
 urgency: nil
 shortest_distance_km: 0
 ferry_time: 0
 ferry_price: 0
 trailer_pos: (0, 0, 0) (1; 0, 0, 0)
 trailer_pos_valid: false
 license_plate: ""
}

I would like the array to look somethink like this:

                    [job_offer] => _nameless.2BB8.4FB8
                    [job_offer_data] => Array
                        (
                            [0] => Array
                                (
                                    [cargo] = null
                                    [company_truck] = ""
                                    [variant] = nill
                                    [target] = ""
                                    etc...

My code at the moment is:

<?php

// Open the file
$fp = @fopen("game.sii", 'r'); 

// Add each line to an array
if ($fp) {
    $array = explode("\n", fread($fp, filesize("game.sii")));
}
print_r($array);
?>

this (as I expected it to) wrote each line of the file into a 1d array however, I would like it to write it to a multidimensional array=.

Even after lots of googling, I just cannot find the solution for how to do this (so I apologise if I have missed something obvious) so I was hoping someone on here could help me.

Thanks in advance, Marcus

Community
  • 1
  • 1
marcus
  • 81
  • 12
  • Where is the data in that file coming from? – Steve Sep 02 '14 at 11:19
  • Hi, what do you mean by coming from. If you mean what is generating it - it is a game save file (that is created by the game) that people will upload and see stats about it – marcus Sep 02 '14 at 11:22

1 Answers1

0

use serialize php function to before write data to the file the write it to the file

and use unserialize php after read data from file

ex :

$str = serialize($array); //then write it to file


$arr = unserialize($str); //after read from file
Osama Jetawe
  • 2,697
  • 6
  • 24
  • 40
  • Hi, Thanks for the answer, however, I seem to be missing something as I don't see how this allows me to put the file into a multidimensional array? Please could you explain (it wasn't the inserting data into the database I was having trouble with - I will updated the question to better reflect this) – marcus Sep 02 '14 at 11:16