Greetings everyone
I have a question about how to dynamically construct a multidimensional array from arbitrary data. A typical example would be an input file like a comma separated text file.
Example Input file:
shops, east coast shop, offerings, cars, honda
shops, east coast shop, offerings, outdoor, tents, terramar
shops, west coast shop, accounting, invoices
and so on. The goal would be to be able to programmatically make use of this data while keeping the associations intact (I know that I could use a database, but thats not what I want to do here :-)
So, I know how to read every line into an array (e.g. with explode), but how do I construct a multidimensional array? How do I accommodate the fact that each line may have a different number of data fields?
As a test, I did something like the following:
$data_struct = array();
$elements= explode(",", $line);
$path = '[' . implode("][", $elements) . ']';
eval('$result = $data_struct' . $path . ';');
return $data_struct;
This works, however, I have the strong feeling that there must be a better way to achieve this goal. Somewhere in the back of my head I remember lots of people frowning upon the use of eval(). Are they right?
Thanx a lot!