Connecting Tech Pros Worldwide Forums | Help | Site Map

array keys

pc
Guest
 
Posts: n/a
#1: Aug 22 '07
I was wondering if there is a more gracious way to write the below
code:

//$input is an array .I just need its 0th and 2nd key-not the 1st.I
want to avoid using temp

$temp=array_keys($input);
print $temp[0];print $temp[2];


Thanks in advance!
PC


ZeldorBlat
Guest
 
Posts: n/a
#2: Aug 22 '07

re: array keys


On Aug 21, 9:58 pm, pc <chavanpr...@gmail.comwrote:
Quote:
I was wondering if there is a more gracious way to write the below
code:
>
//$input is an array .I just need its 0th and 2nd key-not the 1st.I
want to avoid using temp
>
$temp=array_keys($input);
print $temp[0];print $temp[2];
>
Thanks in advance!
PC
Not really. Why is it such a problem to use $temp ?

pakalk
Guest
 
Posts: n/a
#3: Aug 22 '07

re: array keys


On Aug 22, 3:58 am, pc <chavanpr...@gmail.comwrote:
Quote:
I was wondering if there is a more gracious way to write the below
code:
>
//$input is an array .I just need its 0th and 2nd key-not the 1st.I
want to avoid using temp
>
$temp=array_keys($input);
print $temp[0];print $temp[2];
Yeah, to write..

$tmp = array_keys( $input );
echo $tmp[0], $tmp[2];

Ulf Kadner
Guest
 
Posts: n/a
#4: Aug 22 '07

re: array keys


pakalk wrote:
Quote:
$tmp = array_keys( $input );
echo $tmp[0], $tmp[2];
Bad code!

You have always to check if array keys exists before accessing it!
There is no warranty that key 2 exists. Never...
Otherwise you become a error when accessing invalid keys.

$tmp = array_keys($input);
if (sizeof($tmp) >= 3)
echo $tmp[0], $tmp[2];
else
// do alternating things


So long, Ulf

--
_,
_(_p Ulf [Kado] Kadner
\<_)
^^
ZeldorBlat
Guest
 
Posts: n/a
#5: Aug 22 '07

re: array keys


On Aug 22, 7:00 am, Ulf Kadner <dr_lo...@gmx.netwrote:
Quote:
pakalk wrote:
Quote:
$tmp = array_keys( $input );
echo $tmp[0], $tmp[2];
>
Bad code!
>
You have always to check if array keys exists before accessing it!
There is no warranty that key 2 exists. Never...
Otherwise you become a error when accessing invalid keys.
>
I disagree.

$a = array('foo' =0, 'bar' =1, 'baz' =2);
$tmp = array_keys($a);
Quote:
>From that I'll be happy to warrant that $tmp[2] exists.
Steve
Guest
 
Posts: n/a
#6: Aug 22 '07

re: array keys



"Ulf Kadner" <dr_logic@gmx.netwrote in message
news:fah4vj$1vt$01$1@news.t-online.com...
| pakalk wrote:
|
| $tmp = array_keys( $input );
| echo $tmp[0], $tmp[2];
|
| Bad code!

not 'bad' per say...just doesn't explain much using variable names like
$tmp. i'm sure $tmp refers to something meaningful. so, use a variable name
that isn't 'magical'. helps later on when maintaining or enhancing the code.
that's especially true the more $tmp is used later in the code. i also hate
seeing multiple sql resources named $stmt1, $stmt2, etc.. it chaps my hide
having to make sense of that non-scense.

| You have always to check if array keys exists before accessing it!

not 'always'. it depends on your error settings and whether or not you plan
to take action if $input is not an array...like notifying the user. usually
is_array() in this context would only mean a certain state did or did not
exist for this user's data entry, i.e. has/not submitted data. however, he
could just as easily check $tmp[0] or $tmp[2] for that.

$input = is_array($input) ? $input : array($input);

or

$tmp = @array_keys($input);

suffices and only the former checks.

| There is no warranty that key 2 exists.

right, but that may just be what he's banking on.

| Otherwise you become a error when accessing invalid keys.

again, not necessarily.


Ulf Kadner
Guest
 
Posts: n/a
#7: Aug 22 '07

re: array keys


ZeldorBlat wrote:
Quote:
On Aug 22, 7:00 am, Ulf Kadner <dr_lo...@gmx.netwrote:
Quote:
>You have always to check if array keys exists before accessing it!
>There is no warranty that key 2 exists. Never...
>Otherwise you become a error when accessing invalid keys.
>
I disagree.
Not fine ;-)
Quote:
$a = array('foo' =0, 'bar' =1, 'baz' =2);
$tmp = array_keys($a);
>
Quote:
>>From that I'll be happy to warrant that $tmp[2] exists.
The op dont says how the array was build. So its usefull to check always
if it comes with the required data. If you dont doit, your problem.

--
_,
_(_p Ulf [Kado] Kadner
\<_)
^^
Closed Thread