array keys 
August 22nd, 2007, 03:05 AM
| | | |
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 | 
August 22nd, 2007, 03:25 AM
| | | | 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 ? | 
August 22nd, 2007, 07:55 AM
| | | | 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]; | 
August 22nd, 2007, 12:05 PM
| | | | 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
\<_)
^^ | 
August 22nd, 2007, 02:05 PM
| | | | 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.
| | 
August 22nd, 2007, 03:05 PM
| | | | 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. | 
August 22nd, 2007, 04:55 PM
| | | | 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
\<_)
^^ |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 225,689 network members.
|