array keys
am 22.08.2007 03:58:50 von pc
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
Re: array keys
am 22.08.2007 04:16:10 von zeldorblat
On Aug 21, 9:58 pm, pc wrote:
> 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 ?
Re: array keys
am 22.08.2007 08:48:07 von pakalk
On Aug 22, 3:58 am, pc wrote:
> 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];
Re: array keys
am 22.08.2007 13:00:13 von Ulf Kadner
pakalk wrote:
> $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
\<_)
^^
Re: array keys
am 22.08.2007 15:02:47 von zeldorblat
On Aug 22, 7:00 am, Ulf Kadner wrote:
> pakalk wrote:
> > $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);
>From that I'll be happy to warrant that $tmp[2] exists.
Re: array keys
am 22.08.2007 16:02:28 von Steve
"Ulf Kadner" wrote 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.
Re: array keys
am 22.08.2007 17:46:40 von Ulf Kadner
ZeldorBlat wrote:
> On Aug 22, 7:00 am, Ulf Kadner wrote:
>> 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 ;-)
> $a = array('foo' => 0, 'bar' => 1, 'baz' => 2);
> $tmp = array_keys($a);
>
>>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
\<_)
^^