Presentation is loading. Please wait.

Presentation is loading. Please wait.

PHP Arrays Functions.

Similar presentations


Presentation on theme: "PHP Arrays Functions."— Presentation transcript:

1 PHP Arrays Functions

2 Array An array can store many values at once within a single variable.
Types of Arrays in PHP There are three types of arrays that you can create. These are: Indexed array — An array with a numeric key. Associative array — An array where each key has its own specific value. Multidimensional array — An array containing one or more arrays within itself.

3 Indexed Arrays An indexed or numeric array stores each array element with a numeric index. The following examples shows two ways of creating an indexed array, the easiest way is <?php $colors = array("Red", "Green", "Blue"); print_r($colors); ?>

4 Print_r() function The print_r() function is used to print human-readable information about a variable.  the variable is an integer or a float or a string the function returns value of the variable. If the variable is an array the function returns keys and elements.

5 Indexed Arrays-Example
<?php $cname= array ('Pakistan', 'Saudia Arabia','Turkey'); $salary= array (400, 3800,6700); print_r($cname); echo '<br>'; print_r($salary); echo '<hr>'; ?>

6 How to to extract through loops
<?php $colors = array("Red", "Green", "Blue"); for ($a=0; $a<3; $a++) { echo "Name:". $colors[$a]. "<br>"; } echo "<hr>";

7 Foreach Loop syntax The “foreach” loop gives PHP an easy way to iterate over arrays and can only be used on arrays. First syntax foreach ($array as $value) { code to be executed; } Second syntax foreach ($array as $key => $value) { code to be executed;

8 For Loop-indexed array
<?php $colors = array("Red", "Green", "Blue"); for ($a=0; $a<3; $a++) { echo "Name:". $colors[$a]. "<br>"; } Output:

9 Fore each-indexed array
$books= array ("PHP","C++","Java","Perl”,"python"); foreach ( $books as $val) { echo $val. "<br>"; Output: }

10 How to use counter to display data
$counter=1; foreach ( $books as $val) { echo $counter . ". " . $val. "<br>"; $counter++; } Output: echo "<br>";

11 Associative Arrays $ages = array("Nauman"=>22, "Hamid"=>32, "Danish"=>28); foreach ($ages as $x=>$y) { echo "Name: ". $x. "<br>"; echo "Age:" . $y. "<br>"; Output: }

12 Associative array: display in HTML Table
$ages = array("Nauman"=>22, "Hamid"=>32, "Danish"=>28); echo "<table border=1> <tr><td> Name</td> <td> Age</td> </tr>"; foreach ($ages as $x=>$y) { echo "<tr><td>" . $x . "</td>"; echo "<td>" . $y . "</td></tr>"; } echo "</table>"; Output:

13 Multidimensional array
$contacts = array( array( "name" => "Nauman", " " => ), "name" => "Hamid", " " => "name" => "Danish", " " => ) );

14 Multidimension array-Example
$contacts = array( array( "name" => "Nauman", " " => ), "name" => "Hamid", " " => "name" => "Danish", " " => ) );

15 Multidimensional array..con
foreach ($contacts as $contacts_1) foreach ($contacts_1 as $x=>$y ) { echo $x . ":" . $y ."<br>"; } Output:


Download ppt "PHP Arrays Functions."

Similar presentations


Ads by Google