Creating an array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Creating an array

Post by aceconcepts »

Hi,

I am trying to create a variable that can hold an array as an array.

This array is created from multiple checkbox values posted from a form e.g.

Code: Select all

<input type="checkbox" name="chkSession[]" value="value">
.

Once the form is submitted, a loop will be initiated.

Within this loop I want to be able to assign each value from the array to the new array variable.

i.e.

Code: Select all

//ASSUME FORM HAS BEEN POSTED

//START AN FOREACH LOOP TO READ ARRAY VALUES

foreach($_POST["chkSession"] as $chkValue)
{
      $newArrayVar = $chkValue; //THIS IS THE LINE WHERE I WANT TO BE ABLE TO BASICALLY REPLICATE THE ARRAY($chkValue)
}
How would I assign the array values to the new variable as an array?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

$newArrayVar = $_POST['chkSession'];
Make sure it exists first. ;)
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Post by aceconcepts »

Ok, superb.

Just wanted to make sure. Thanks.
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Post by aceconcepts »

There is one more thing:

If i'm trying to add values to this array within a loop, how do I append the new array variable so that the new array holds more and more values throughout the loop?

This is how I've done it so far:

Code: Select all

//THIS IS THE NEW ARRAY DEFINITION
$sessionArray=array();

//THE BELOW CODE GETS THE VALUE FROM EACH RADIO ELEMENT THAT RELATES TO A SINGLE CATEGORY ID FROM A DB
					$dbGetCatCount=mysql_query("SELECT DISTINCT intSessionDateTimeId FROM otblSessionDate") or die(mysql_error());
					$dbCatCount=mysql_num_rows($dbGetCatCount);
					
						while($row=mysql_fetch_array($dbGetCatCount))
						{
							extract($row);
								
								if(isset($_POST["chk" . $row["intSessionDateTimeId"]]))
								{
									$sessionArray .= $_POST["chk" . $row["intSessionDateTimeId"]];
								}
						}
When I print $sessionArray to the screen it displays as: Array14

1 and 4 being the radio element values.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Arrays don't use string concatenation to add to them. Have you read through the manual on this? http://php.net/language.types.array
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Post by aceconcepts »

Sorry, I'm being a bumbaclut.

I have been throught the link you posted and I can't seem to find out how to append array values to a variable within a loop.

This is what I have:

Code: Select all

$dbGetCatCount=mysql_query("SELECT DISTINCT intSessionDateTimeId FROM otblSessionDate") or die(mysql_error());
					$dbCatCount=mysql_num_rows($dbGetCatCount);
					
						while($row=mysql_fetch_array($dbGetCatCount))
						{
							extract($row);
							$i++;
								if(isset($_POST["chk" . $row["intSessionDateTimeId"]]))
								{									
									$sessionArray = array($prevSession, $_POST["chk" . $row["intSessionDateTimeId"]]);
								}
						}
Basically I'd like to use ".=" so that the next value in the loop would append to the already existing $sessionArray. However, I know ".=" does not work with arrays.

The values will be dynamically generated so I will never know how many there will be.

Do you see my dilemma?
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Post by aceconcepts »

I've sorted it.

I was being a bit thick.

I wrote the array variable as

Code: Select all

$sessionArray = array($prevSession, $_POST["chk" . $row["intSessionDateTimeId"]]);
As opposed to the correct way:

Code: Select all

$sessionArray[] = $prevSession, $_POST["chk" . $row["intSessionDateTimeId"]];
Thanks for your attention Feyd.

Much appreciated (once again) :wink:
Post Reply