Tricky array question

Tricky array question

am 07.11.2007 20:58:11 von jamesgoode

Hi,

I've got a complicated problem to solve. Say I have an indexed array
$array, with contents as follows:

'name' => 'James'
'favefood' => 'Spagbol'
'os' => 'Debian GNU/Linux'

And say I want to create a list, that prints the name of each item,
and then its contents, like this:

name: James
favefood: Spagbol
os: Debian GNU/Linux

The only problem, I don't what the items in the array are called, so I
can't do this:

os:

Any ideas? It's got me stumped.

Thanks in advance,

--James Goode.

Re: Tricky array question

am 07.11.2007 21:02:50 von Good Man

jamesgoode wrote in news:1194465491.529347.23280
@d55g2000hsg.googlegroups.com:

> Hi,
>
> I've got a complicated problem to solve. Say I have an indexed array
> $array, with contents as follows:
>
> 'name' => 'James'
> 'favefood' => 'Spagbol'
> 'os' => 'Debian GNU/Linux'
>
> And say I want to create a list, that prints the name of each item,
> and then its contents, like this:
>
> name: James
> favefood: Spagbol
> os: Debian GNU/Linux
>
> The only problem, I don't what the items in the array are called, so I
> can't do this:
>
> os:
>
> Any ideas? It's got me stumped.
>
> Thanks in advance,

foreach($array as $key=>$value) {
echo "$key: $value
";
}

Also, as mentioned elsewhere, future-proof your code and lose the short
tags... no more , and only

Re: Tricky array question

am 07.11.2007 21:09:51 von jamesgoode

On Nov 7, 8:02 pm, Good Man wrote:
> jamesgoode wrote in news:1194465491.529347.23280
> @d55g2000hsg.googlegroups.com:
>
>
>
> > Hi,
>
> > I've got a complicated problem to solve. Say I have an indexed array
> > $array, with contents as follows:
>
> > 'name' => 'James'
> > 'favefood' => 'Spagbol'
> > 'os' => 'Debian GNU/Linux'
>
> > And say I want to create a list, that prints the name of each item,
> > and then its contents, like this:
>
> > name: James
> > favefood: Spagbol
> > os: Debian GNU/Linux
>
> > The only problem, I don't what the items in the array are called, so I
> > can't do this:
>
> > os:
>
> > Any ideas? It's got me stumped.
>
> > Thanks in advance,
>
> foreach($array as $key=>$value) {
> echo "$key: $value
";
>
> }
>
> Also, as mentioned elsewhere, future-proof your code and lose the short
> tags... no more , and only

Hi Good Man,

Thanks for your fast response, now I can continue with my project :-).

Regards,

--James

Re: Tricky array question

am 08.11.2007 05:13:51 von Jake Barnes

On Nov 7, 2:58 pm, jamesgoode wrote:
> Hi,
>
> I've got a complicated problem to solve. Say I have an indexed array
> $array, with contents as follows:
>
> 'name' => 'James'
> 'favefood' => 'Spagbol'
> 'os' => 'Debian GNU/Linux'
>
> And say I want to create a list, that prints the name of each item,
> and then its contents, like this:
>
> name: James
> favefood: Spagbol
> os: Debian GNU/Linux
>
> The only problem, I don't what the items in the array are called, so I
> can't do this:
>
> os:
>
> Any ideas? It's got me stumped.

I often do this:

while (list($key, $val) = each($array)) {
echo "os: $key ";
}


If I have an array just input by a form, and I need to addslashes
before sending it to a database, this is quite useful:

while (list($key, $val) = each($formInputs)) {
$formInputs[$key] = addslashes($val);
}