simple array question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
iffo
Forum Commoner
Posts: 37
Joined: Thu Oct 05, 2006 11:56 am

simple array question

Post by iffo »

Hi, I want to make an array with only indexes no values, it is possible to do something like this

$validfields['name'];
$validfields['age'];


or will the sytax be


$validfields['name']="";
$validfields['age']="";
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Why?

It'd be the latter, more than the former.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

or simply...

Code: Select all

$var = array('name' => '', 'age' => '');
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
iffo
Forum Commoner
Posts: 37
Joined: Thu Oct 05, 2006 11:56 am

Post by iffo »

i have a logic where i am only interested in the indexes of the array, someone told me

$validfields['name'];
$validfields['age'];

but if i knew it was not correct, if i do print_r($validfields), it does not print anything , but on the other one
$validfields['name']="';
$validfields['age']="';



it prints.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

If you don't need the values at all, why have named indexes? Why not just place the names as values?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

Agreed. I'm not sure conceptually what the difference between the following if you are not going to use the value in the associative one:

Code: Select all

array('name' => '', 'age' => ''); 

// and

array('name', 'age');
(#10850)
Post Reply