Php-mysql Course

Arrays are variables used to store multiple values in a single variable name, each value being a string or anumber or even another array.
Arrays are structured as a series of key-value pairs, where one pair is an element of that array. For each item in the list, there is a key (or index) associated with it.
In PHP there are two kind of Arrays: Numeric Arrays (known as indexed arrays) and Associative Arrays.

Numeric Array

A numeric array (or indexed array) stores each array element with a numeric index /key (generally, consecutive, and starting at 0).
There are two primary methods to create a numeric array.

1) Creating Array with the square bracket syntax, adding one element at a time.
  - Syntax:
$array[0] = value0;
$array[1] = value1;
$array[2] = value2;
 ...
The key (within square bracket) can start at any number.
- You can use this method in the fallowing way too (without adding the keys manually):
$array[] = value0;
$array[] = value1;
$array[] = value2;
 ...
In this way, PHP automatically assigns the keys, starting at 0 (value0 will have associated the index 0).
  - Example:
<?php
// specifying the keys for each element
$array[0] = 'php';
$array[1] = 'mysql';
$array[2] = 8;

// without specifying the keys
$fruits[] = 'apple';
$fruits[] = 'banana';
$fruits[] = 'apricots';
?>

2) Creating Array with the array() function, adding the key=>value pairs in one step, and separated by commas.
  - Syntax:
$array = array(0=>value0, 1=>value1, 2=>value2);
The first key can start at any number. You can add as many key=>value pairs as you want (separated by commas).
- You can use this method in the fallowing way too (without adding the keys manually):
$array = array(value0, value1, value2);
In this way, PHP automatically assigns the keys, starting at 0 (value0 will have associated the index 0).
  - Example:
<?php
// specifying the keys for each element
$array = array(0=>'php', 1=>'mysql', 2=>8);

// without specifying the keys
$fruits = array('apple', 'banana', 'apricots');
?>

The keys of any array have to be named uniquely, otherwise each identical key assigned will replace the one before it.
If you want to create an empty array, just use the array function without any parameters:
                    $arr = array();

Accessing a specific Array's element

To access the value of a specific element of an array start with the array variable name, followed by the key in square brackets:
$var_name = $array[key];
The value of the array element with the specified key will be stored in the $var_name variable.
  - Example:
<?php
$array = array('php', 'mysql', 8);

$elm = $array[1];
echo $elm. '<br />';           // mysql

echo $array[0]. ' course';               // php course
?>
In this example the keys aren't added when the array is defined, but we know that PHP will assign automatically the keys, starting at 0.
The code above will output:
mysql
php course

To get the number of elements in an array you can use the count() function:
                    $nr = count($array);

Modify and Add values in an Array

To modify a value in an array, assign a new value to the element with a specified index number.
  - Syntax:
array_name[index] = 'new value';
If the array_name[index] element isn't defined in that array, will be added automatically with the 'new value'. This method can be used when you want to add new elements in an array.
  - Example:
<?php
// define an array
$fruits = array('apple', 'banana', 'apricots');

// change the value of first element
$fruits[0] = 'figs';

$nr = count($fruits);              // gets the number of items in $fruits
echo 'There is '. $nr. ' elements in the array. The first is: '. $fruits[0];

// adds a new item in $fruits
$fruits[] = 'nuts';

$nr = count($fruits);              // gets again the number of items in $fruits
echo '<br /> Now, the $fruits array has '. $nr. ' items';
?>
To add a new element in a numeric array, you can also use the syntax:   $array[] = value; (for example:   $fruits[] = 'nuts';).
This code will output:
There is 3 elements in the array. The first is: figs
Now, the $fruits array has 4 items

Traversing numeric Arrays

To access every element in a numeric array, use the for() loop instruction.
  - Syntax:
$nr = count($array);        // gets the number of elements
for($i=0; $i<$nr; $i++) {
  // PHP instructions for the curent element:  $array[$i];
}
  - Example:
<?php
$arr = array('php', 'mysql', 8);

$nr = count($arr);
for($i=0; $i<$nr; $i++) {
  $elm = $arr[$i];
  echo '<br /> Item '. $i. ' has the value: '. $elm;
}
?>
Output;
Item 0 has the value: php
Item 1 has the value: mysql
Item 2 has the value: 8

Associative Arrays

Associative arrays use strings in place of index numbers, to associate the value of the elements.
There are two methods to create an associative array.

1) Creating Array with the square bracket syntax, adding one element at a time.
  - Syntax:
$array['key1'] = value1;
$array['key2'] = value2;
$array['key3'] = value3;
 ...
You can use either single or double quotes.
  - Example:
<?php
$sites['en'] = 'www.courseweb.net';
$sites['ro'] = 'marplo.net';
$sites['search'] = 'www.google.com';
?>

2) Creating Array with the array() function, adding the 'key'=>value pairs in one step, and separated by commas.
  - Syntax:
$sites = array('key1'=>value1, 'key2'=>value2, 'key3'=>'value3');
You can add as many 'key'=>value pairs as you want (separated by commas).
  - Example:
<?php
$sites = array('en'=>'www.courseweb.net', 'ro'=>'marplo.net', 'search'=>'www.google.com');
?>

You can refer to a specific element of an associative array, to access or change its value, the same as you would an indexed array, using the key string instead of a number.
  - Example:
<?php
// define the array
$sites['ro'] = 'www.courseweb.net';
$sites['en'] = 'marplo.net';
$sites['search'] = 'www.google.com';

echo 'Search: '. $sites['search'];             // Search: www.google.com

// change the value of 'search' item
$sites['search'] = 'www.yahoo.com';

echo '<br /> Search: '. $sites['search'];      // Search: www.yahoo.com
?>
This code will output:
Search: www.google.com
Search: www.yahoo.com

Traversing associative Arrays

You can walk through, or traverse, an associative array with the foreach() loop.
  - Syntax:
foreach ($array as $key => $val) {
  // Do something with $key and /or $val.
}
This construct will place the key and value of the current $array element into their own variables: $key and $val.
It will allow you to step through each element of an array and implement code on its key and value.
  - Example:
<?php
// define the array
$sites['ro'] = 'www.courseweb.net';
$sites['en'] = 'marplo.net';
$sites['search'] = 'www.google.com';

// traversing $sites
foreach ($sites as $key => $val) {
  echo '<br /> The key is: '. $key. ' and its value is: '. $val;
}
?>
This code will output:
The key is: ro and its value is: www.courseweb.net
The key is: en and its value is: marplo.net
The key is: search and its value is: www.google.com


• Starting with version PHP 5.4 has been added a new way to define arrays, using short array syntax.
E.g.
$arr = [];              // empty array, instead of array();

$arr = [1, 2, 3, 4];    // numeric array

$arr = ['ro'=>'courseweb.net', 'en'=>'marplo.net', 'num'=>8];       // associative array
- The array created with this short syntax can be accessed, modified and traversed as any array.

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which HTML element can be used to embed a SWF flash content?
<object> <div> <script>
<object type="application/x-shockwave-flash" data="file.swf" width="500" height="250">
 <param name="src" value="file.swf" />
 Your browser not support SWF.
</object>
Which CSS pseudo-class adds a style to an input form field that has keyboard input focus?
:active :focus :hover
input:focus {
  background-color: #88fe88;
}
Click on the instruction which converts a JSON string into a JavaScript object.
JSON.stringify(javascript_object) object.toString() JSON.parse(json_string)
var jsnstr = '{"url": "http://coursesweb.net/", "title": "Web Development Courses"}';
var obj = JSON.parse(jsnstr);
alert(obj.url);
Indicate the PHP function which can be used to create or write a file on server.
fopen() file_put_contents() file_get_contents()
if (file_put_contents("file.txt", "content")) echo "The file was created";
else echo "The file can not be created";
PHP Arrays

Last accessed pages

  1. Get and Modify content of an Iframe (32367)
  2. $_GET, $_POST and $_REQUEST Variables (33884)
  3. Ajax-PHP Chat Script (49508)
  4. JavaScript Course - Free lessons (31647)
  5. Volume and Surface Area Calculator for 3D objects (11276)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (310)
  2. The Mastery of Love (48)
  3. CSS cursor property - Custom Cursors (36)
  4. Read Excel file data in PHP - PhpExcelReader (35)
  5. PHP-MySQL free course, online tutorials PHP MySQL code (31)