- HubPages»
- Technology»
- Computers & Software»
- Computer Science & Programming»
- Programming Languages
PHP Code Example on Associative Array and Multidimensional Array
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
- How to become a PHP Developer: Learn and Earn
- Steps on How to Program in PHP: A Beginner's Guide on PHP Programming-Part 1
- How to run PHP Code or PHP Scripts and See the Result on Your Browser
- HTML Basics: Learn The Basics of HTML and Create Your Own Webpage on Your Browser
- PHP Sample Codes On Function:How to make Function in PHP
- PHP Code Example on Foreach Loop and For Loop
- PHP code Example for if statement
- PHP Code Example for if-else Statement
- PHP Code Example on if else-if Statement
- PHP Code Example on Switch Statement
- PHP Code Example on While Loop
- PHP Code Example on Numeric Array
- PHP Code Example on Do-while Loop