Creating and updating associative dynamic array for translations

Again, you do not understand text files and how you are planning on using them is totally wrong.

First, you want to load text into a file so that makes it larger. But, you want to edit the file live, so that means
a complicated process to determine the end-of-lines in the PHP file. Which you want to be live programming
text which then gets re-compiled into an array when the page loads. That is a ton of work. just use the array
that you are creating anyways and store it with my last two lines of code. All PHP files are loaded into memory
when they are called. So, you waste the server’s time loading a text file, turning it into php file text and then
have the server convert the text into an array in memory instead of just saving and loading it using PHP functions
created just for that use.

The object here is that you want to slow down your page and have us create a way to do the code badly.
If you don’t want us to help you do this correctly, okay. Then, read the hex values of your file and figure out
what the end-of-line characters are. Then you can write code to use the string_replace() function to remove
the second line. You would have to use the string_position() function to find the EOL and then use that and
the position of the next EOL and replace the data between them with “” (null text) and that will work. BUT, it
is so slow compared to manipulating your array and storing it, perhaps about 100 times a slow and if you have
a large number of users on the server, it will waste server resources and slow the server down. Your choice!

The joy of making things more difficult! You want to use a file, fine, but at least have that file be xml and not a straight text document.

Yep, I was going to suggest that, but, since he didn’t want to use any other form, I skipped over it. The two
very simple commands I gave him will load and save the array to a file with ease. Deleting the first row is easy
and adding one to the end is simple too. If this was April 1st, I would get it… LOL

It is not xml nor 'really" text it is php

I am about half there now .

I think you guys should pay more attention to what people ask for and keep opinions to yourselves . I am not interested in changing the format of the file AT ALL as the file format is already referenced in the core code in too many places.

Sometimes you make a solution that fits the need (existing code) instead of rewriting everything.

And by the way not one of you guys even showed how to insert a line in an ASSOCIATIVE array file which is not a numbered array, and not an array in memory.

I am having great success working with the array file as a text file

I just joined this party but I think I’ve got the jist of what you’re trying to do.

I don’t think you need to consider performance at this stage, if the file is in use it will probably be in (file system) cache anyway, and looking into this now would probably be considered premature optimizations. I’m quite confident this file can grow quite a bit without much performance issues.
[php]<?php

// Load translation cache
$lang = array(
‘entry1’ => “this would be the translation for entry1”,
‘entry2’ => “this would be the translation for entry2”,
);

// Remove from translation cache
unset($lang[‘entry1’]);

// Add to translation cache
// Warning: This will replace the content of $lang[‘new_key’] if it exists
$lang[‘new_key’] = ‘New value’;

// On the flip side this could be used to update language keys as well
$lang[‘entry2’] = ‘Updated translation for entry2’;

// Update cache file
// Save this string instead of echoing it
echo '<?php
$lang = ’ . var_export($lang, true) . ‘;’;

/*
Output
ps: you don’t need to have ? > at the end of files

<?php $lang = array ( 'entry2' => 'Updated translation for entry2', 'new_key' => 'New value', ); */[/php] If you do it like this you will have to use the method above to update/save the file every time you make changes to the array.

Nothing quite like tight coupling to really drive that home is there?

I don’t know the structure of the ‘file’. In your first post you showed how you change the values in an associative array, I thought that was already known.

[php]$ar[‘test’] = ‘some string’;
echo “

{$ar[‘test’]}

”;
$ar[‘test’] = ‘some other string’;
echo “

{$ar[‘test’]}

”;[/php]

I thought the goal was for this to be dynamic?

Well, I give up on this one.

I showed how to remove the top item of an array and add one to the bottom.
I showed how to save and retrieve the entire array in just two lines of code.
I explained all of the pros and cons for using text files and he still wants to use them, hence the solutions I gave.

Done…

Sponsor our Newsletter | Privacy Policy | Terms of Service