Assigning Value of and Array Element as Key to Another Array

0 views
Skip to first unread message

Pavan PHP

unread,
Aug 17, 2010, 3:27:42 AM8/17/10
to professi...@googlegroups.com
Hi All

This is a situation which comes to me while coding in php day in day out. I am trying to improve the way I write the code so looking for you suggestion. Please go through this and suggest me, If you have any idea how to do this a better way.

I have a $RAW['userid'] which contains actual user id of the user.

I have another array $USER[] which contains all the users id and their relevant values.

Suppose $RAW['userid'] have value 'pavan', I need to check whether $USER['pavan'] is set or not.

To do this, I use following code:

$temp_user_id = $RAW['userid'];
if(isset($USER["$temp_user_id"])) { echo "User Exists"; }

Is there any way to get this done without creating a new variable $temp_user_id?

Regards
Pavan PHP

Jack Timmons

unread,
Aug 17, 2010, 6:42:22 AM8/17/10
to professi...@googlegroups.com
On Tue, Aug 17, 2010 at 2:27 AM, Pavan PHP <pava...@gmail.com> wrote:
> $temp_user_id = $RAW['userid'];
> if(isset($USER["$temp_user_id"])) { echo "User Exists"; }

if(!empty($USER[$RAW['userid']]))

--
Jack Timmons
@_Codeacula

umakantpatil

unread,
Aug 17, 2010, 8:18:41 AM8/17/10
to Professional PHP Developers
I think you could write

if(isset($USER[$RAW['userid']])) { echo "User Exists"; }

Regards
Umakant

Ovidiu Alexa

unread,
Aug 17, 2010, 8:26:51 AM8/17/10
to professi...@googlegroups.com
Hi,

could you use something like this?

echo ($USER[$RAW['userid']]<>'')?'User Exists':'';

or

$string=($USER[$RAW['userid']]<>'')?'User Exists':'';
echo $string;

or as you want

$string=(isset($USER[$RAW['userid']]))?'User Exists':'';
echo $string;


Cheers!

> --
> This group is managed and maintained by the development staff at 360
> PSG. An enterprise application development company utilizing
> open-source technologies for todays small-to-medium size businesses.
>
> For information or project assistance please visit :
> http://www.360psg.com
>
> You received this message because you are subscribed to the Google
> Groups "Professional PHP Developers" group.
> To post to this group, send email to Professi...@googlegroups.com
> To unsubscribe from this group, send email to
> Professional-P...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/Professional-PHP

Reply all
Reply to author
Forward
0 new messages