Connecting Tech Pros Worldwide Help | Site Map

array keys

  #1  
Old August 22nd, 2007, 03:05 AM
pc
Guest
 
Posts: n/a
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

  #2  
Old August 22nd, 2007, 03:25 AM
ZeldorBlat
Guest
 
Posts: n/a

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 ?

  #3  
Old August 22nd, 2007, 07:55 AM
pakalk
Guest
 
Posts: n/a

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];

  #4  
Old August 22nd, 2007, 12:05 PM
Ulf Kadner
Guest
 
Posts: n/a

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
\<_)
^^
  #5  
Old August 22nd, 2007, 02:05 PM
ZeldorBlat
Guest
 
Posts: n/a

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.
  #6  
Old August 22nd, 2007, 03:05 PM
Steve
Guest
 
Posts: n/a

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.


  #7  
Old August 22nd, 2007, 04:55 PM
Ulf Kadner
Guest
 
Posts: n/a

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Fill array keys if it doesn't exists Sjoerd answers 2 September 8th, 2008 10:15 PM
Foreach loop with Array Keys The.Relinator@gmail.com answers 2 December 12th, 2006 01:55 AM
using array_intersect() without keeyping keys from the array1, i.e. re-number intersect array keys. duzhidian@gmail.com answers 1 July 29th, 2006 01:35 AM
sorting an associative array keys based on values soup_or_power@yahoo.com answers 5 July 23rd, 2005 09:57 PM