ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel

PHP Code Example on Associative Array and Multidimensional Array

Updated on February 22, 2013

PHP code, PHP example, An example source code for php associative and multidimensional array.

Arrays are very important in every programming languages. In this hub you will see another example of php code on the other types of array, the associative and the multidimensional array. You will see the difference between the two and you will learn on how to use them in your coding on a PHP Editor.

Please note that in coding PHP, you must have a PHP editor like notepad++, a php server like apache and much better if you have a database server too like mysql. To have both apache and mysql you might want to download xaamp for free just google it.

To run the sample source codes you must, at a least, know how to run a php code in your browser. If you have a problem on running a php code on your browser, please refer to the link below for some simple easy steps on executing php codes or php scripts.


How to run a PHP code on your browser using XAAMP and Notepad++.

If you do not know how to run an HTML code on your browser, you may check the following link too.

Learn the basic of HTML: How to create an offline webpage

To know more about associative and multidimensional arrays, refer to the source code below. You can study and trace the codes and learn how the outputs happen.

Sample PHP Code for Associative Array

<?php
	$student = array("J. Parker" => "ID Number: 00120", "N. Smith"=> "ID Number: 00121", 
	"M. Maclein" => "ID Number: 00122", "A. Garfield" => "ID Number: 00123", "R. Rutherford" => "ID Number: 00124");
	$count = count($student);
	
	echo "<strong>Associative Array Example Output</strong>". "<br/><br/>";
	
	
	echo "Student Name: J. Parker  ".$student['J. Parker']. "<br/>";
	echo "Student Name: N. Smith  ".$student['N. Smith']. "<br/>";
	echo "Student Name: M. Maclein  ".$student['M. Maclein']. "<br/>";
	echo "Studet Name: A. Garfield  ".$student['A. Garfield']. "<br/>";
	echo "Student Name: R. Rutherford ".$student['R. Rutherford']. "<br/>";
	
?>

Sample Output for Associative Array

Sample PHP Code of Multidimensional Array

<?php
	$section = array("Section A" => array("J. Parker", "N. Smith", "M. Maclein"), 
	"Section B" => array("R. Rutherford", "A. Swan", "L. Go"), 
	"Section C" => array("C. Wilson", "N. Tucker", "Z. Page"));
	
	$count = count($section);
	
	echo "<strong>The students on Section A Are: </strong><br />";
	for($i=0; $i<$count; $i++)
	{
		echo $section['Section A'][$i]. "<br />";
	}
	
	echo "<br />";
	echo "<strong>The students on Section B Are: </strong><br />";
	for($i=0; $i<$count; $i++)
	{
		echo $section['Section B'][$i]. "<br />";
	}
	
	echo "<br />";
	echo "<strong>The students on Section C Are: </strong><br />";
	for($i=0; $i<$count; $i++)
	{
		echo $section['Section C'][$i]. "<br />";
	}
?>

Sample Output for Multidimensional Array

Comments

    0 of 8192 characters used
    Post Comment

    No comments yet.

    Click to Rate This Article