Toon posts:

[PHP] een array echoen met key en al

Pagina: 1
Acties:
  • 39 views sinds 30-01-2008

Verwijderd

Topicstarter
wat zitten surfen op php.net, maar werd er niet echt wijzer uit

stel je voor :
$array[blah]=1;
$array[blah2]=1;
$array[blah3]=2;

hoe kan ik dit echoen:
blah1 - 1
blah2 - 1
blah3 - 2

?

  • .oisyn
  • Registratie: September 2000
  • Laatst online: 09-09 22:48

.oisyn

Moderator Devschuur®

Demotivational Speaker

code:
1
2
foreach ($array as $key => $value)
    echo $key . " - " . $value;

ik kan trouwens niet geloven dat jij door te surfen op php.net dit niet kan vinden :?

Give a man a game and he'll have fun for a day. Teach a man to make games and he'll never have fun again.


Verwijderd

Op maandag 08 april 2002 01:01 schreef OiSyN het volgende:
PHP:
1
<?foreach ($array as $key => $value)    echo $key . " - " . $value;?>

ik kan trouwens niet geloven dat jij door te surfen op php.net dit niet kan vinden :?
ej cool
maar ff een vraag:
PHP:
1
<?$array[blah] = "1<br>";$array[blah2] = "1<br>";$array[blah3] = "2<br>";foreach ($array as $key => $value){    echo $key . " - " . $value;    }?>

waarom pakt ie die br niet zonder " ?

  • DizzyWeb
  • Registratie: Februari 2001
  • Laatst online: 06-09 00:14

DizzyWeb

Ondertiteld

Op maandag 08 april 2002 01:11 schreef DJ-Jumbo het volgende:

[..]

ej cool
maar ff een vraag:
PHP:
1
<?$array[blah] = "1<br>";$array[blah2] = "1<br>";$array[blah3] = "2<br>";foreach ($array as $key => $value){    echo $key . " - " . $value;    }?>

waarom pakt ie die br niet zonder " ?
Ik hoop dat ik je vraag niet snap... Want zover ik het snap vraag je waarom er quotes om een string heen moeten? :?

  • .oisyn
  • Registratie: September 2000
  • Laatst online: 09-09 22:48

.oisyn

Moderator Devschuur®

Demotivational Speaker

werkt bij mij gewoon hoor :?

dat stukje code van jou geeft bij mij de volgende output:
code:
1
2
3
blah - 1
blah2 - 1
blah3 - 2

of begrijp ik je nu verkeerd

Give a man a game and he'll have fun for a day. Teach a man to make games and he'll never have fun again.


  • tomato
  • Registratie: November 1999
  • Niet online
wh00t OiSyN aan de PHP 8-)

En hoe! Direct 100% correcte antwoorden :D

  • .oisyn
  • Registratie: September 2000
  • Laatst online: 09-09 22:48

.oisyn

Moderator Devschuur®

Demotivational Speaker

:7

.edit: dat wordt me nieuwe sig :)
.edit2: het past overigens precies bij mijn ondertitel :D (no offense voor de topicstarter ;))

Give a man a game and he'll have fun for a day. Teach a man to make games and he'll never have fun again.


Verwijderd

:)

ik bedoel meer...
waarom pakt ie de 1 wel en als ik er <br> achter zet
pakt ie uhm niet...
ja je begrijpt de vraag goed...
ik weet dat het moet alleen ik ben gewoon nieuwsgierig WAAROM het moet :)

Verwijderd

Op maandag 08 april 2002 01:16 schreef OiSyN het volgende:
werkt bij mij gewoon hoor :?

dat stukje code van jou geeft bij mij de volgende output:
code:
1
2
3
blah - 1
blah2 - 1
blah3 - 2

of begrijp ik je nu verkeerd
ik weet:
test

maar zonder de quotjes niet :)

  • .oisyn
  • Registratie: September 2000
  • Laatst online: 09-09 22:48

.oisyn

Moderator Devschuur®

Demotivational Speaker

nou, zonder de quotes is het een getal.

Als je er een <br> achter wilt dan moet het als string (een getal kan tenslotte geen <br> bevatten). Vandaar de quotes eromheen :)

Give a man a game and he'll have fun for a day. Teach a man to make games and he'll never have fun again.


Verwijderd

Op maandag 08 april 2002 01:22 schreef OiSyN het volgende:
nou, zonder de quotes is het een getal.

Als je er een <br> achter wilt dan moet het als string (een getal kan tenslotte geen <br> bevatten). Vandaar de quotes eromheen :)
mjah als ik er nu over nadenk is het wel een hele domme vraag :D

thnx :)

  • .oisyn
  • Registratie: September 2000
  • Laatst online: 09-09 22:48

.oisyn

Moderator Devschuur®

Demotivational Speaker

zie mijn ondertitel 8-)

Give a man a game and he'll have fun for a day. Teach a man to make games and he'll never have fun again.


Verwijderd

Topicstarter
thanx guys, verstond niet juist wat er op php.net stond ...
heh

  • tomato
  • Registratie: November 1999
  • Niet online
OiSyN: :7

.edit: dat wordt me nieuwe sig :)
Alsjeblieft :* ;)

  • Sircuri
  • Registratie: Oktober 2001
  • Niet online

Sircuri

Volledig Appelig

PHP maakt het jullie zo makkelijk dat jullie er gewoon overheen lezen. ZOEKEN is the keyword!!!

http://www.php.net/manual/en/function.print-r.php

makkelijker kan je het niet maken

Signature van nature


  • drm
  • Registratie: Februari 2001
  • Laatst online: 09-06-2025

drm

f0pc0dert

Sircuri:
print_r

makkelijker kan je het niet maken
*kuch* Da's een debug-functie. Als je er opeens een tabel van wilt maken wordt 't wel een stukkie moeilijker (lees: minder makkelijk), hm? ;)

Music is the pleasure the human mind experiences from counting without being aware that it is counting
~ Gottfried Leibniz


  • Grum
  • Registratie: Juni 2001
  • Niet online
Nu wil ik, als error_reporting(E_ALL) fan, nog eventjes een klein korreltje zout bijdragen aan deze 'discussie' :D

Als je named array gebruikt dan [b]MOET[/b] (ok, ok, behoor :D) je de keys tussen quotes zetten. Simpelweg omdat het strings zijn en die behoren tussen quotes te staan.

Nu kan je wel fijn zeggen 'ja maar het werkt toch' of 'ja maar ik gebruik geen error_reporting(E_ALL)' als je dat zegt ... tja ... ghehe .. dan heb ik geen goed woord voor je over :D

Als je niet quote geeft PHP deze warning:
Warning: Use of undefined constant XXXXXX - assumed 'XXXXXX' in XXXXXXX on line X
Wat ie dus gaat doen:
1/ kijken of er een define bestaat
2/ warning geven
3/ toch maar een string ervan maken en gebruiken

Dit is dus enorm 'zielig' en inefficient en dit alleen maar omdat er een paar quotes 'vergeten' zijn.

Dus de eerst post moet zijn:
code:
1
2
3
$array['blah']  = 1;
$array['blah2'] = 1;
$array['blah3'] = 2;

En dat was wel weer genoeg 'preek' voor vandaag :+

  • drm
  • Registratie: Februari 2001
  • Laatst online: 09-06-2025

drm

f0pc0dert

* drm snapt ook niet waarom mensen pertinent WEIGEREN om quotes te gebruiken bij indices

Ik zie het zooo vaak, en het wordt toch duidelijk afgeraden in de manual:
uit: PHP Manual / Arrays / Why is foo[bar] wrong?

Why is $foo[bar] wrong?
You might have seen the following syntax in old scripts:
code:
1
2
3
$foo[bar] = 'enemy';
echo $foo[bar];
// etc



This is wrong, but it works. Then, why is it wrong? The reason is that, as stated in the syntax section, there must be an expression between the square brackets ('[' and ']'). That means that you can write things like this:
code:
1
echo $arr[ foo(true) ];

This is an example of using a function return value as the array index. PHP knows also about constants, and you may have seen the E_* before.
code:
1
2
3
$error_descriptions[E_ERROR] = "A fatal error has occured";
$error_descriptions[E_WARNING] = "PHP issued a warning";
$error_descriptions[E_NOTICE] = "This is just an informal notice";

Note that E_ERROR is also a valid identifier, just like bar in the first example. But the last example is in fact the same as writing:
code:
1
2
3
$error_descriptions[1] = "A fatal error has occured";
$error_descriptions[2] = "PHP issued a warning";
$error_descriptions[8] = "This is just an informal notice";



because E_ERROR equals 1, etc.

Then, how is it possible that $foo[bar] works? It works, because bar is due to its syntax expected to be a constant expression. However, in this case no constant with the name bar exists. PHP now assumes that you meant bar literally, as the string "bar", but that you forgot to write the quotes.

So why is it bad then?
At some point in the future, the PHP team might want to add another constant or keyword, and then you get in trouble. For example, you already cannot use the words empty and default this way, since they are special keywords.

And, if these arguments don't help: this syntax is simply deprecated, and it might stop working some day.
Als dat niet duidelijk is, dan weet ik het niet meer ...

Music is the pleasure the human mind experiences from counting without being aware that it is counting
~ Gottfried Leibniz


  • Grum
  • Registratie: Juni 2001
  • Niet online
Es ff in me sig verwerken :D

  • tomato
  • Registratie: November 1999
  • Niet online
Grum: Es ff in me sig verwerken :D
;( ;)

  • drm
  • Registratie: Februari 2001
  • Laatst online: 09-06-2025

drm

f0pc0dert

tomato:
;( ;)
Whaa :D ik heb nog wel een plekkie over, hoor :* ;)

Music is the pleasure the human mind experiences from counting without being aware that it is counting
~ Gottfried Leibniz


  • Grum
  • Registratie: Juni 2001
  • Niet online
Op maandag 08 april 2002 14:24 schreef tomato het volgende:
;( ;)
Momentje :D

  • D2k
  • Registratie: Januari 2001
  • Laatst online: 31-08 10:19

D2k

Doet iets met Cloud (MS/IBM)

Pagina: 1

Dit topic is gesloten.