Arrays
 

Arrays are a great and simple way to store info, but it's worth it to "pop the hood" so you know what you're doing in certain scenarios.

A great resource, by the way is W3 Schools.

I) Simple Array

     A) Indexed Array

In its most basic form, an array is going to look like this:
	 $result_array = array(); //define the array		
	 $result_array=array("snare", "tom", "bass");
	
What you're looking at here is an "indexed" array in that you don't have any specified
keys
. We'll get into that later.

So, you've got one variable with mulitiple variables stored inside. Behold, the magic of the array.

To print or "echo" the results as they're coded above, you could use either a "for" loop, or you could simply spit them out according to the way in which they're systemically stored. Like this:

To spit them out using an index, that would look like this:
	<php
	 $result_array = array(); //define the array		
	 $result_array=array("snare", "tom", "bass");
	 echo $result_array[0];
	 //you get "snare"
	 ?>
	
Using a "for" loop, you do it this way:
	<?php
	 $result_array = array(); //define the array		
	 $result_array=array("snare", "tom", "bass");
	 $count=count($result_array);
	 
	 for($i=0; $i<=$count-1; $i++)
	 {
		 echo $result_array[$i].'<br>';
	 }
	 //you get snare, tom, bass
	?>
	
     B) Associative Array

An Associative Array is what happens when you identify every member of your array with a "key." That's going to look like this:
  
	$result_array = array(); //define the array		
	$result_array=array("snare"=>"piccolo", "tom"=>"ride", "bass"=>"kick");
	echo $result_array['snare'];
	 //result is "piccolo"
	 

If you wanted to loop through what you've got in your array, you would do it like this:
	$result_array = array(); //define the array		
	$result_array=array("snare"=>"piccolo", "tom"=>"ride", "bass"=>"kick");
	foreach($result_array as $drum=>$drum_value)
	{
		echo "Key=" .$drum. ", Value=" .$drum_value;
		echo "<br>>";
	}	
	//the result is:
	Key=snare, Value=piccolo
	Key=tom, Value=ride
	Key=bass, Value=kick
	
Cool!

II) Multidimensional Array

The Multidimensional Array is a situation where you have an array within an array.

The easiest way to envision it is a table:

drum detail
snare piccolo
tom ride
kick 24 inch


Savvy?

Now...here's the way it looks like right now in the context of your PHP array:
	$kit=array(
	array("snare", "piccolo"),
	array("tom", "ride"),
	array("kick", "24 inch")
	);
	

In order to retrieve the values from within that array, you have to utilize two indices. That's going to look like this:
	$kit=array(
	array("snare", "piccolo"),
	array("tom", "ride"),
	array("kick", "24 inch")
	);
	$statement= "Your first drum referenced in your \"kit\" array is your ";
	$statement.=$kit[0][0];
	$statement.=" and it is a ";
	$statement.=$kit[0][1];
	echo $statement;
	//the resulting statement is "Your first drum referenced in your "kit" array is your snare and it is a piccolo.
	

The other way to do it is a for loop within a for loop which will look like this:

	$kit=array(
	array("snare", "piccolo"),
	array("tom", "ride"),
	array("kick", "24 inch")
	);

	$count_main=count($kit);
	$count_drum=count($kit[0]);

	for($x=0; $x<$count_main; $x++)
	{
		for($y=0; $y<$count_drum; $y++)
		{
			echo $kit[$x][$y].'<br>';
		}
	}
	
snare
piccolo
tom
ride
kick
24 inch


Here's how your array can be envisioned in the context of the way it is typically produced by a "while" dynamic in PHP


So, here's what you've got, typically, when you're retrieving a list of rows from a database in PHP...

<?php
$sql=$conn->prepare("SELECT * from database"); $sql->execute(); while($row=$sql->fetch(PDO::FETCH_ASSOC)) { $result[]=$row; } //now you're retrieving that $result knowing that it's an multidimensional array foreach($result as $data) { echo $data['id']; // if you were envisioning this as a table, this would the "id" column of every row echo "<br>"; } ?>


If you want to run a "proof" of something, just to see if how that PHP dynamic is going to play out, then here's how you would do it:

Begin by setting up your experimental array: