Forum Moderators: coopster
I have an array and need to have each item of the array (each key?) replaced depending on the language settings of the user. The language is set in a variable called '$lang'.
As an example, I can do this with a non-array variable like this:
$lang = 'english';
$LANG['english']['Balcón'] = 'Balcony ';
$LANG['french']['Balcón'] = '@@';
$LANG['german']['Balcón'] = '@@';
$LANG['spanish']['Balcón'] = 'Balcón';
$test = "Balcón";
print $LANG[$lang][$test];
And get it to print 'Balcony', however I can't seem to do this with an array.
In the end, I need to take the array, replace the values, and then put it back together again as an array.
Any ideas?
I'm going to explain my confusion and add some details that may help me and others to understand this:
My array comes from a database. It is stored as:
somefield= Value1¦Value2¦Value3¦etc.
The array is created by exploding the value from somefield:
$my_array = explode("¦",$row[somefield]); Then, the values are built into a list as below:
foreach($my_array as $key => $value){
if ($value) $mylist .= "<li>$value</li>\n"; "if $my_array contains 'flat', add 'piso' to $my_array_es" for instance. Then apply the 'foreach' outlined above to the new array.
Blech - I'm crosseyed with this one... and I bet there is an easy way out.
To work out my "your logic to assign the correct $newvalue to the array key being processed" I seem to be doing something wrong. Here's what I've got:
$my_array: Array ( [0] => a [1] => c [2] => e [3] => g [4] => )
foreach($my_array as $key => $value){
if ($value = 'a') {$newvalue[] = 'b';} elseif
($value = 'c') {$newvalue[] = 'd';} elseif
($value = 'e') {$newvalue[] = 'f';} elseif
($value = 'g') {$newvalue[] = 'h';}} What this returns is:
$newvalue: Array ( [0] => b [1] => b [2] => b [3] => b [4] => b )
I must have some simple principle wrong here, but I've read up and tried some random 'doesn't make sense but I'll give it a try' stuff. No luck. Any ideas?
foreach($my_array as $key => $value){
if ($value = 'a') {$my_array[$key] = 'b';} elseif
($value = 'c') {$my_array[$key] = 'd';} elseif
($value = 'e') {$my_array[$key] = 'f';} elseif
($value = 'g') {$my_array[$key] = 'h';}}
I had tried that, but the same problem.
I stuck this
<?php
$my_array = array(0 => "a","c","e","g" );foreach($my_array as $key => $value)
{
if ($value = 'a') {$my_array[$key] = 'b';} elseif
($value = 'c') {$my_array[$key] = 'd';} elseif
($value = 'e') {$my_array[$key] = 'f';} elseif
($value = 'g') {$my_array[$key] = 'h';}}
print_r($my_array);
?>
into a file with nothine else, just to see what would happen, and I get this:
Array ( [0] => b [1] => b [2] => b [3] => b )
(but in normal sized letters ;-] ) I'm going to search around a bit, but I'm open to ideas!
$my_array = array('a', 'c', 'e', 'g');
$my_array = array(
'key1' => 'a',
'key2' => 'c',
'key3' => 'e',
'key4' => 'g');
Search for "jon at nospam dot com"
Basically, it seems that the "internal array pointer is not incremented on the original array as the Note in the documentation for foreach says."
I'm gonna muck around some more... Thanks for the help!
- BTW, good to keep in mind (maybe?) that the "$my_array" I'll be using will come from
$my_array = explode("¦",$row[somefield]);
$my_array = array('a', 'c', 'e', 'g');
while (list($key, $value) = each($my_array)) {
if ($value = 'a') {$my_array[$key] = 'b';} elseif
($value = 'c') {$my_array[$key] = 'd';} elseif
($value = 'e') {$my_array[$key] = 'f';} elseif
($value = 'g') {$my_array[$key] = 'h';}
}
This is exactly what I was after though, at least in theory. If the key would advance I'd be laughing....
I don't have access to my test server right now, but will in a couple of hours. I'll test the code and post a follow-up then.
Above and beyond, thanks that'd be great. I'm in Spain, and lights out soon, so if I don't post back till tomorrow, that's why.
Thanks again!
$my_array = array('a', 'c', 'e', 'g');
while (list($key, $value) = each($my_array)) {
if ($value == 'a') {$my_array[$key] = 'b';} elseif
($value == 'c') {$my_array[$key] = 'd';} elseif
($value == 'e') {$my_array[$key] = 'f';} elseif
($value == 'g') {$my_array[$key] = 'h';}
}
foreach($my_array as $key => $value){
if ($value == 'a') {$my_array[$key] = 'b';} elseif
($value == 'c') {$my_array[$key] = 'd';} elseif
($value == 'e') {$my_array[$key] = 'f';} elseif
($value == 'g') {$my_array[$key] = 'h';}}
Still no luck - check this out:
h*tp://es2.php.net/foreachSearch for "jon at nospam dot com"
Basically, it seems that the "internal array pointer is not incremented on the original array as the Note in the documentation for foreach says."
current function will return the expected results. But, as I stated before, foreach operates on a copy of the original array. Try this with his code, substituting a while loop instead of his foreach loop:
$someArray = array('1', '2', '3', '4');
// foreach ($someArray as $curNum) {
while (list($curNum) = each($someArray)) {
echo $curNum . " => " . current($someArray) . "\n<br />";
}
and it returns:
0 => 2
1 => 3
2 => 4
3 =>