Jump to content

Assign Fields


EchoFool

Recommended Posts

Is there a quick method to assigning fields to an Array ?

 

Say i have SELECT * FROM table

 

Now say theres 30 field, i don't want to be doing:

 

$UserID = $row['UserID'];$Username = $row['Username'];  for all 30 fields.

 

Is there a way to loop them into an array? Would save so much time.

Link to comment
Share on other sites

What's wrong with $row['UserID']? It is a perfectly valid variable.

 

By assigning it to another variable $UserID = $row['UserID'], you are just wasting processing time and memory.

 

Okay good point on that front - Judging from your answer that means the loop option is not possible ? :P

Link to comment
Share on other sites

Okay ill do a small example

 

Table:

UserID | Username | Password | Email

---------------------------

 

Query:

 

SELECT * FROM table

 

Now in php currently i do:

 

$array = array($row['UserID'],$row['Username']  etc etc

 

But if theres some 30 + fields in the table, it gets a bit annoying having to write it all out, so i wanted to see if i can create the array from while looping some how - so that i don't have to type out giant arrays all the time. The reason i put them in an array is because this occurs in a function.

 

Which i then place the array in return().

Link to comment
Share on other sites

I'm not sure I agree with what you're trying to do, but this will do it:

$array=array();

foreach ($row as $key=>$value) {

$array[$key]=$value;

}

 

I will give it a try - may i ask why you don't agree?

 

Because you likely don't need it.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.