id2341677
id2341677

Reputation: 319

Processing an array into multiple files correctly with PHP

All right so I have a problem and I'm looking for the best way to model it.

At the moment I have an array called $lang and it is defined in multiple files. It is initialized as so in each file: $lang = array_merge($lang, array( "Key" => "Value", )); so that when multiple files are included on a page, the $lang array contains all keys and values from their respective files into one big array.

Now I want to build a front-end where it displays all of the attributes from the array, a user can change the attributes, and save them. At the moment I am including them as so:

foreach(glob("language/*.php") as $filename){
   include $filename;
}

I display them all fine, but when I want to re-submit them as a form, I don't know how to specify which Key => Value belonged to which file, as they were all merged when they were included.

Is there some clever way I can differentiate which file a certain Key => Value belonged to as I have set it up right now, or should I step back and set up the model differently?

Upvotes: 1

Views: 270

Answers (5)

hakre
hakre

Reputation: 197732

The main problem I see with your code is that you hardencode the $lang variable plus some functional magic inside the data-file(s). Consider the following instead to differ more between data and logic:

language/sample.php:

return array("key" => "value");

loading script:

foreach(glob("language/*.php") as $filename){
   $filedata = include($filename);
   $lang[$filename] = $filedata;
   # - OR -
   $lang = array_merge($lang, $filedata);
}

You can now use the language data-files more modular because they are not bound to $lang any longer. For example to display an editor per file. Or to add the needed meta-data as well.

Upvotes: 1

rkulla
rkulla

Reputation: 2524

sounds like you need to store the filename in each array using a multidimensional array, eg

array("filename"=>array("Key" => "Value")

Upvotes: 2

GWW
GWW

Reputation: 44093

Perhaps you could make some kind of language key to filename mapping:

$map = array();
foreach(glob("language/*.php") as $filename){
   $lang = array();
   include $filename;
   foreach($lang as $k=>$v){
       $map[$k] = $filename;
   }
}

EDIT:

But it's probably a better idea to refactor your code and use some of the other answers suggestions.

Upvotes: 2

eykanal
eykanal

Reputation: 27017

The two ways I can think of this are sorting by file:

array(
    'filename' => array(
        'key' => 'value',
    )
)

or sorting by key:

array(
    'key' => array(
        'value',
        'filename'
    )
)

It really depends on how you want to deal with it later. I don't think there's a "correct" answer here.

Upvotes: 1

konsolenfreddy
konsolenfreddy

Reputation: 9671

your input fields could look something like this, with the filename in them:

<input type="text" name="data[file1][key1]" value="new value" />
<input type="text" name="data[file2][key1]" value="new value" />

That way you can differentiate them and write the files back in different files.

Upvotes: 1

Related Questions