PHP working with arrays
Last update on: 10-08-2008There are tow types of arrays nominatives and associative to create an array we will use the array() function.
Example for an array nominative:
$table = array('itw','php5','mysql'); // we set the values of the table print $table; //output array
Example for an associative array:
$ass_table = array(site=>'iTW',language=>'php',base=>'mysql'); // we set the table as name_of_variable=>'value' //to display print 'the name of the website is '.$ass_table['site']; //output the name of the website is iTW
Understanding how the arrays works
The rows of tables are not numbered as you think, the first record bears the line number 0, so a table like this:$table = array('value1','value2','value3');
print $table['0']; //output value1
Using looping functions to display all the values:
with the foreach() function, for php4 only
$table = array('iTW','php4','mysql'); //we set the table and its elements foreach ( $table as $content ) //we on display all the values { print $content.'<br />';//we print the result on the screen } Output iTW php4 mysql
The same thing with an associative array:
$table = array(site=>'iTW',language=>'php4',base=>'mysql'); //we define the table and its elements foreach ( $table as $key=>$content ) //we call the table by assigning the variable key { print 'Key '.$key.' value '.$content.'<br / >'; //we display the key and its content } Output Key site value iTW Key language value php4 Key base value mysql
Add an element to an array
//we define the table and its elements $table = array('iTW','php4','mysql'); //We add with the function array_push() array_push($table,"java-script"); //The new array $tableau = array('iTW','php4','mysql','java-script');
PHP and MySQL's lessons:
Introduction To PHPGet Started With PHP
PHP Variables
PHP Variables Of Environment
PHP Conditions
PHP Looping
PHP Cookies
PHP Working With Dates
PHP Working With Arrays
PHP Working With Files
PHP Play With Strings
PHP And Forms
Send Emails With PHP
The Include Statement
Get Started With MySQL
MySQL Update And Delete
The WHERE Clause
MySQL Functions
Guestbook Script
Websites Directory Script
Multiple Pages With PHP
Create Your Forum With Php

