Accessing string arrays

Accessing string arrays

am 25.01.2008 21:44:37 von Giovanni

Hi! I've an array of string like this:

$arraystring["name"]="John";
$arraystring["age"]="50";
$arraystring["work"]="teacher";

$arraystring is global variable.

I'm trying to code a function that get a string as parameter and print
the relative arraystring's value.

function printArrayStr($key)
{
global $arraystring;
echo $arraystring["$key"];
}

What do I wrong?! :(

tnx, people!

Re: Accessing string arrays

am 25.01.2008 23:30:06 von Steve

"Giovanni" wrote in message
news:bfe96afe-98dc-4850-98a8-890b0de68daf@j20g2000hsi.google groups.com...
> Hi! I've an array of string like this:
>
> $arraystring["name"]="John";
> $arraystring["age"]="50";
> $arraystring["work"]="teacher";
>
> $arraystring is global variable.
>
> I'm trying to code a function that get a string as parameter and print
> the relative arraystring's value.
>
> function printArrayStr($key)
> {
> global $arraystring;
> echo $arraystring["$key"];
> }
>
> What do I wrong?! :(

well, for one, you don't need a function for that. last time i checked,
this:

echo $arraystring['name'];

took less effort than:

printArrayStr('name');

second, functions should be *generic* !!! if your function only works on
this one variable, what good is it? if would be better to pass the array and
the key to the function...that way, the function's importance remains with
its ability to 'print' an array value. ex., what good would php's print_r()
function be if it only worked on one data type, or if it relied on a global
variable. get it?

finally, you don't need quotes around $key in your function's echo
statement.

as for debugging your specific code...here's the answer. delete everything
starting with 'f' in 'function', down to and including the ending function
bracket. then use this:

echo $arraystring['name']; // or whatever key you want to echo from the
array.

Re: Accessing string arrays

am 26.01.2008 00:08:30 von Giovanni

I'm making a multilanguage app.
so I store the translation in an array...
Using the function system, I can check in a clean mode, if the
variable exist, is empty, or something else, and choose a solution
(i.e. ###string not translated ###
Do you understand what do I mean?

Re: Accessing string arrays

am 26.01.2008 01:32:05 von Steve

"Giovanni" wrote in message
news:1647b620-ddc2-4b23-8d1b-9653fc16e816@n20g2000hsh.google groups.com...
> I'm making a multilanguage app.
> so I store the translation in an array...
> Using the function system, I can check in a clean mode, if the
> variable exist, is empty, or something else, and choose a solution
> (i.e. ###string not translated ###
> Do you understand what do I mean?

i understand what you mean, however, you're going about it the wrong way.

Re: Accessing string arrays

am 26.01.2008 09:05:04 von Giovanni

Really?Is it not a good idea to store translation data into an array
indexed by string? In this way,if i want to add a language,i only need
to create a new file and translate only the array values. However if
you have any different idea,is appreciated :-)

Re: Accessing string arrays

am 26.01.2008 10:17:27 von Michael Fesser

..oO(Giovanni)

>I'm making a multilanguage app.
>so I store the translation in an array...

You could have a look at the gettext extension.

Micha

Re: Accessing string arrays

am 26.01.2008 10:48:05 von Giovanni

On 26 Gen, 10:17, Michael Fesser wrote:
> .oO(Giovanni)
>
> >I'm making a multilanguage app.
> >so I store the translation in an array...
>
> You could have a look at the gettext extension.
>
> Micha

mmm....interesting library... I never heard before...but
I should plan some changes in my code...
I could think at this solution in next week,
but now I need find a solution to main problem at top
of this topic, because I've to show a preview in 4 hours :(

Re: Accessing string arrays

am 27.01.2008 05:24:06 von Peter Pei

How amazing this is. His code worked with 5.2.5 for sure. Obviously none of
the previous repliers cared to test his code, and went ahead with stupid
answers any way.

His code works 100% as long as he calls it the right way, for example
printArrayStr("age"). Obviously he will get notice if he calls
printArrayStr(age).

make sure your answers is relevant, otherwise you are playing pure stupid.
=============================================
"Giovanni" wrote in message
news:bfe96afe-98dc-4850-98a8-890b0de68daf@j20g2000hsi.google groups.com...
> Hi! I've an array of string like this:
>
> $arraystring["name"]="John";
> $arraystring["age"]="50";
> $arraystring["work"]="teacher";
>
> $arraystring is global variable.
>
> I'm trying to code a function that get a string as parameter and print
> the relative arraystring's value.
>
> function printArrayStr($key)
> {
> global $arraystring;
> echo $arraystring["$key"];
> }
>
> What do I wrong?! :(
>
> tnx, people!

Re: Accessing string arrays

am 27.01.2008 13:30:24 von Giovanni

I told _interesting library_... Not that I'll use it now!
I wanted to create the function in 1st post, just to solve this kind
of problem.
I pass the string parameter, the function checks for errors, so it
returns the correct value (string translated), or a default one.
Someone can tell me why my code doesn't work?

Re: Accessing string arrays

am 27.01.2008 15:55:05 von Jerry Stuckle

Giovanni wrote:
> Hi! I've an array of string like this:
>
> $arraystring["name"]="John";
> $arraystring["age"]="50";
> $arraystring["work"]="teacher";
>
> $arraystring is global variable.
>
> I'm trying to code a function that get a string as parameter and print
> the relative arraystring's value.
>
> function printArrayStr($key)
> {
> global $arraystring;
> echo $arraystring["$key"];
> }
>
> What do I wrong?! :(
>
> tnx, people!
>

First of all, don't use globals. If you want to pass it to the
function, pass it to the function. As a global, $arraystring could be
changed anywhere, and you'll have a hard time figuring out what the
problem is.

But are you sure this is *exactly* like you have it in your code? For
one thing, you shouldn't have quotes around $key as the index.

But you never told us what your problem is, so it's impossible to tell
you what the cause is. For instance, Enable all error reporting and
display errors - do you get any error messages? What's actually in
$arraystring and $key in the function?

And finally - I agree - this is probably one of the $*worst* ways to
handle multiple languages.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

Re: Accessing string arrays

am 27.01.2008 15:59:07 von Jerry Stuckle

Peter Pei wrote:
> "Giovanni" wrote in message
> news:bfe96afe-98dc-4850-98a8-890b0de68daf@j20g2000hsi.google groups.com...
>> Hi! I've an array of string like this:
>>
>> $arraystring["name"]="John";
>> $arraystring["age"]="50";
>> $arraystring["work"]="teacher";
>>
>> $arraystring is global variable.
>>
>> I'm trying to code a function that get a string as parameter and print
>> the relative arraystring's value.
>>
>> function printArrayStr($key)
>> {
>> global $arraystring;
>> echo $arraystring["$key"];
>> }
>>
>> What do I wrong?! :(
>>
>> tnx, people!
>
>
> How amazing this is. His code worked with 5.2.5 for sure. Obviously
> none of the previous repliers cared to test his code, and went ahead
> with stupid answers any way.
>
> His code works 100% as long as he calls it the right way, for example
> printArrayStr("age"). Obviously he will get notice if he calls
> printArrayStr(age).
>
> make sure your answers is relevant, otherwise you are playing pure
> stupid.
> =============================================

(Top posting fixed)

At least the others didn't top post.

And no, I didn't test his code because he didn't post where the problem
is.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

Re: Accessing string arrays

am 28.01.2008 04:57:30 von Steve

"Jerry Stuckle" wrote in message
news:Iqydnfz_u548BgHanZ2dnUVZ_gadnZ2d@comcast.com...
> Giovanni wrote:
>> Hi! I've an array of string like this:
>>
>> $arraystring["name"]="John";
>> $arraystring["age"]="50";
>> $arraystring["work"]="teacher";
>>
>> $arraystring is global variable.
>>
>> I'm trying to code a function that get a string as parameter and print
>> the relative arraystring's value.
>>
>> function printArrayStr($key)
>> {
>> global $arraystring;
>> echo $arraystring["$key"];
>> }
>>
>> What do I wrong?! :(
>>
>> tnx, people!
>>
>
> First of all, don't use globals. If you want to pass it to the function,
> pass it to the function. As a global, $arraystring could be changed
> anywhere, and you'll have a hard time figuring out what the problem is.
>
> But are you sure this is *exactly* like you have it in your code? For one
> thing, you shouldn't have quotes around $key as the index.
>
> But you never told us what your problem is, so it's impossible to tell you
> what the cause is. For instance, Enable all error reporting and display
> errors - do you get any error messages? What's actually in $arraystring
> and $key in the function?
>
> And finally - I agree - this is probably one of the $*worst* ways to
> handle multiple languages.

thank you very much, jerry. we finally agree on something. :)