1

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?

0

5 Answers 5

2

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

array("filename"=>array("Key" => "Value")
Sign up to request clarification or add additional context in comments.

Comments

2

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.

Comments

1

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.

1 Comment

I ended up using this setup for my fields, so thank you. I wish I could select two accepted answers!
1

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.

Comments

1

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.

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.