PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.php > Tricky array question
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Tricky array question

Réponse
 
LinkBack Outils de la discussion
Vieux 07/11/2007, 19h58   #1
jamesgoode
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Tricky array question

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: <?=$array['os']?>

Any ideas? It's got me stumped.

Thanks in advance,

--James Goode.

  Réponse avec citation
Vieux 07/11/2007, 20h02   #2
Good Man
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Tricky array question

jamesgoode <james@jgoode.co.uk> 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: <?=$array['os']?>
>
> Any ideas? It's got me stumped.
>
> Thanks in advance,


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

Also, as mentioned elsewhere, future-proof your code and lose the short
tags... no more <?= $myVar ?>, and only <?php echo $myVar; ?>

  Réponse avec citation
Vieux 07/11/2007, 20h09   #3
jamesgoode
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Tricky array question

On Nov 7, 8:02 pm, Good Man <he...@letsgo.com> wrote:
> jamesgoode <ja...@jgoode.co.uk> 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: <?=$array['os']?>

>
> > Any ideas? It's got me stumped.

>
> > Thanks in advance,

>
> foreach($array as $key=>$value) {
> echo "$key: $value <br>";
>
> }
>
> Also, as mentioned elsewhere, future-proof your code and lose the short
> tags... no more <?= $myVar ?>, and only <?php echo $myVar; ?>


Hi Good Man,

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

Regards,

--James

  Réponse avec citation
Vieux 08/11/2007, 04h13   #4
lawrence k
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Tricky array question

On Nov 7, 2:58 pm, jamesgoode <ja...@jgoode.co.uk> 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: <?=$array['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);
}



  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 11h23.


Édité par : vBulletin® version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,14471 seconds with 12 queries