Forum Moderators: coopster

Message Too Old, No Replies

Lookup and replace values of an array

Pulling apart, manipulating, and reconstructing an array....

         

mipapage

2:15 pm on Aug 25, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hello all...

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?

mipapage

2:53 pm on Aug 25, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ideas:

The array will vary in how many values that it includes.

If I can assign each value of the array to a variable, then have each variable 'looked up and replaced' via "$LANG[$lang][$test];" and then put it back together into an array... that might work.

coopster

3:43 pm on Aug 25, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



You're on the right track, they are called multi-dimensional arrays. It takes a bit of getting used to, but once you have them down they are very useful and will fit your application. See Example 6-10. Recursive and multi-dimensional arrays on the PHP reference pages [us3.php.net ]

mipapage

4:29 pm on Aug 25, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



coopster,

Brilliant! Though not what I expected, I can see this being quite useful.

Thanks for pointing me in the right direction!

mipapage

6:59 pm on Aug 25, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hmmm.. Though still very cool, I'm not sure if the above will get the job done here...

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";



What I need to do is take the values from $my_array and assign them a new value. I suppose the way to this would be to use a second array, like this:

"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.

coopster

7:35 pm on Aug 25, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



To update the value of the existing array as you are requesting:

foreach ($my_array as $key => $value) {
$my_array[$key] = $newvalue;
}

Of course, you would need to supply your logic to assign the correct $newvalue to the array key being processed.

mipapage

8:11 pm on Aug 25, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Okay coopster, I'm learning. Thanks.

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?

coopster

8:22 pm on Aug 25, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Close, very close. But you need to update the array and key that you are processing (assuming you still want to update the original array, not create a new one):

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';}}

mipapage

9:38 pm on Aug 25, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hey coopster,

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!

coopster

9:46 pm on Aug 25, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Oh, I missed that before. You are setting up your test array incorrectly. Try setting up your array using commas, like this, then test your code again:

$my_array = array('a', 'c', 'e', 'g');

PHP will assign numeric indices to the array for you. If you wanted to assign your own keys, it would look something like this:

$my_array = array(
'key1' => 'a',
'key2' => 'c',
'key3' => 'e',
'key4' => 'g');

mipapage

9:56 pm on Aug 25, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Still no luck - check this out:
h*tp://es2.php.net/foreach

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]);

coopster

10:05 pm on Aug 25, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



You know what...that's probably because foreach operates on a copy of the original array. Perhaps using a while() loop is better in this instance.

$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';}
}

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.

mipapage

10:27 pm on Aug 25, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Still the same coopster, it's like the key doesn't advance, and keeps replacing $my_array[0].

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!

coopster

12:10 am on Aug 26, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



This was easy, and a common mistake...
Your if statements are assigning, not comparing! Therefore, after assigned 'a' to the $value, the if statement was evaluating as true, and assigning 'b' to the next element in the array as the foreach was traversing the array! It should be as follows (notice the '==' to compare for equality):

$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';}
}

OR

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';}}

Either way works!

coopster

12:51 am on Aug 26, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



And as far as...

Still no luck - check this out:
h*tp://es2.php.net/foreach

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 not completely sure on this, but mixing the array traversal constructs seems to be a bad idea. If you use the while/each construct, the
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 =>

mipapage

10:28 am on Aug 27, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



coopster,

Thanks for this, I'll give it a shot. Sorry that I didn't reply sooner, I was offline for a day :-O

A huge thanks if it works! (I have to test later, bit of an emergency so I'm ofline again real soon.)

Thanks for the help - mipapage

mipapage

1:15 pm on Aug 27, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



coopster,

It works perfectly, a whole bunch of thank you's and gratitude heading your way. This was the last thing I needed to get licked for converting a script to multi-language, and I learnt a ton doing it.

Happy coding...