Creating and updating associative dynamic array for translations

It means that it moves it DOWN in the indexing… Therefore, index 0 becomes whatever index 1 was. Or in an
example it would be like doing $index[0]=$index[1]; $index[1]=$index[2]; etc… So, index 0 goes away.
Adding to the index would add it at the end of the index…

So, up and down depends on the aspect of the viewer. Down in PHP.net meant down in indexes. No up in a list
that is in your mind. Lower indexes always mean down…

Hope that makes sense… Just test it out. Set up just three or four test items in an array and shift them and
use a print_r($array) to see what is lest in it…

Oh, also… If you want to keep it at 1000, you can use the count() function to count the items in the array.
Or, just do this using your database and keep a higher amount…

thanks but I do not get enough out of this to even try. They assume that my array is a single field I suppose and show no other syntax examples.

My array fields are like

‘leg’ => “pierna”,

Their examples are for single fields (numbered?)

Use a database for long term storage. Query the database for a phrase and language, return that value. The other option, create a localization json file and output based on the language. If you are already storing a translated value, there is no point in retranslating it.

Well, I mentioned moving to a database table for this as Astonechiper also said. But, for doing this in an array
the array_sift function will remove the first array entry. If it is “something”=>“else” this entire item is removed.
And, the second entry would become that first one. Did you test it?

This is NOT long term storage. These are classified ads that will expire.

As it is I am having trouble on the path that I am on now you suggest I go to a database which would require a massive code rewrite?

I am asking about the array as a text file as the code already supports. Please do not deviate from my needs.

All the more reason for database storage. Lets say the columns, id, lang1, lang2, loadedTimestamp

It loads, gets translated and stored till removal.

i am not rewriting the existing language calls in ALL of the code, so NO to the database and if you do not understand I say again NO DATABASE

please stop the database rant this question is NOT about a database

Well, just use the simple code functions I have told you about. They are simple and do what you want!
Did you try them.? NO! Or, you would already have the code working. Again, here is another sample to make
you understand how array_shift works. Just load this in one blank test page and see what you get out of it…

[php]

<?php $lang = array( "orange"=>"la-orange", "banana"=>"la-banana", "apple"=>"la-la-apple" ); echo "Original array:
"; print_r($lang); array_shift($lang); echo "

array-shifted one item:
"; print_r($lang); $lang["ernie"] = "alex"; echo "

added another item:
"; print_r($lang); ?>

[/php]
As easy as I can explain it to you… Good luck with it…

Been testing having zero luck with adding lines to the array.

Will check your examples better here

The problem I am having now is with adding the line to the array (file) and making it the right format array_push

Thanks

no clue actually I am lost. Array Push only appends the array in memory only as does the array shift so neither of these is working with the actual file rather the loaded array. I need to work with the file.

Well, you are confusing us again… Why did you change a simple array to using array_push. That’s a stack
function. And, you never talked about a file? Do you mean that you are attempting to keep the array inside
of a website file? If so, how are you loading the file? Is it a CSV file? A text file or are you saving the array
directly with a file write command? There are hundreds of ways to store data into files, but, in most cases, it
is a slow way to handle data. Especially if it changes often.

If you are talking about saving the array in a file, then you can convert to JASON or just dump it like this:
file_put_contents(“Myfile.xyz”, var_export($array, true));

Not tested, just loosely off the top of my head… Or you can serialize it and write it as a standard text file
and then unserialize it when it comes back in. Lots of ways… All depends on what you want it to do…

After some thought before bedtime…

Just use the array as I posted. Then, if needed to save it into a file, just use this version:

file_put_contents(“filename.xyz”, serialize($array));

That saves a text-savable version of the array. Then, to read it back do it like this:

$array = array();
$array = unserialize($file_get_contents(“filename.xyz”));

Simple… Just remember this is not a good way to handle data. It uses much more server resources than
a few lines of database code…

Some of you confused yourselves with your database mindset. I said from the beginning I am working with an array on a text file. The text file is where I need to focus my attention

loading the entire array in memory seems intensive, especially if later I make it much larger.

text operations could work in this case and some do not need to load the whole file in memory

Here is what I have come to so far but nothing I have tested has worked

I treat my en.inc.php and es.inc.php as a text files (spanish and english respectively.

I want to delete line 2 and append the file before the closing " )?> " tags in this case at line 999

A predefined file length of 1000 would be great as that will give me 998 lines of cached translations, and that is enough for now.

I can concatenate the text strings easily enough to enter the new line at line 2 (after line 999 is deleted ) and insert the new string at line 2

what I need are text string operations that do the following

  1. delete line 999 (not leaving a blank line rather deleting the entire line)
  2. insert new translation line at line 2

done

I do not believe that string operators care about the php at line 1 and line 1000

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?

Sponsor our Newsletter | Privacy Policy | Terms of Service