PHWinfo banniere

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

Réponse
 
LinkBack Outils de la discussion
Vieux 18/09/2007, 20h50   #1
kj
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut php show variable from array in output

I searched around as much as I could for this, let me know if anyone
has some advice. I would like to actually output the name of the
variable instead of the value.

<snip>
$requiredInt=array($amnt,$bNum,$rNum,$oNum,$numPay ments);
$i=0;

foreach ($requiredInt as $key => $val) {
if (!filter_var($val, FILTER_VALIDATE_INT)) { echo('Item: '.!Output
the variable name here!.' is required and must be a numeric integer,
please go back and make this correction before submitting.<br />'); }
$i++;
}
unset($requiredInt);
</snip>

Any advice is appreciated. Thank you.
garrett

  Réponse avec citation
Vieux 19/09/2007, 05h35   #2
sysop073@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: php show variable from array in output

On Sep 18, 3:50 pm, kj <kils...@gmail.com> wrote:
> I searched around as much as I could for this, let me know if anyone
> has some advice. I would like to actually output the name of the
> variable instead of the value.
>
> <snip>
> $requiredInt=array($amnt,$bNum,$rNum,$oNum,$numPay ments);
> $i=0;
>
> foreach ($requiredInt as $key => $val) {
> if (!filter_var($val, FILTER_VALIDATE_INT)) { echo('Item: '.!Output
> the variable name here!.' is required and must be a numeric integer,
> please go back and make this correction before submitting.<br />'); }
> $i++;}
>
> unset($requiredInt);
> </snip>
>
> Any advice is appreciated. Thank you.
> garrett


I'm not certain what you're asking, but I think you want to take
$foo='bar' and get 'foo' out of it, which I'm almost certain can't be
done. The easiest way to get around your problem is to set the key of
each item equal to the name:
$requiredInt=array('amnt'=>$amnt,'bNum'=>$bNum,'rN um'=>$rNum,'oNum'=>
$oNum,'numPayments'=>$numPayments);

Alternatively, if you want to avoid the repetition, you can just set
the array to the string names:
$requiredInt=array('amnt','bNum','rNum','oNum','nu mPayments');

Then in the foreach you can use $val to get the name of the variable
and $$val to get its value.

  Réponse avec citation
Vieux 19/09/2007, 13h27   #3
kj
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: php show variable from array in output

On Sep 19, 12:35 am, sysop...@gmail.com wrote:
> On Sep 18, 3:50 pm, kj <kils...@gmail.com> wrote:
>
>
>
> > I searched around as much as I could for this, let me know if anyone
> > has some advice. I would like to actually output the name of the
> > variable instead of the value.

>
> > <snip>
> > $requiredInt=array($amnt,$bNum,$rNum,$oNum,$numPay ments);
> > $i=0;

>
> > foreach ($requiredInt as $key => $val) {
> > if (!filter_var($val, FILTER_VALIDATE_INT)) { echo('Item: '.!Output
> > the variable name here!.' is required and must be a numeric integer,
> > please go back and make this correction before submitting.<br />'); }
> > $i++;}

>
> > unset($requiredInt);
> > </snip>

>
> > Any advice is appreciated. Thank you.
> > garrett

>
> I'm not certain what you're asking, but I think you want to take
> $foo='bar' and get 'foo' out of it, which I'm almost certain can't be
> done. The easiest way to get around your problem is to set the key of
> each item equal to the name:
> $requiredInt=array('amnt'=>$amnt,'bNum'=>$bNum,'rN um'=>$rNum,'oNum'=>
> $oNum,'numPayments'=>$numPayments);
>
> Alternatively, if you want to avoid the repetition, you can just set
> the array to the string names:
> $requiredInt=array('amnt','bNum','rNum','oNum','nu mPayments');
>
> Then in the foreach you can use $val to get the name of the variable
> and $$val to get its value.


Your assumption about what I tried to do was right. I tried your
second solution, to avoid the repetition. It works perfectly and
solved my issue. Thank you very much!

  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 23h20.


Édité par : vBulletin® version 3.7.3
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 ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,09999 seconds with 11 queries