class 4 - php arrays

33
PHP Arrays

Upload: ahmed-swilam

Post on 16-Jan-2017

483 views

Category:

Technology


13 download

TRANSCRIPT

Page 1: Class 4 - PHP Arrays

PHP Arrays

Page 2: Class 4 - PHP Arrays

Outline

Page 3: Class 4 - PHP Arrays

What are arrays ?

• Arrays are data structures that contain a group of elements that are accessed through indexes.

• Usage :<?php

$x = array( “banana”, “orange”, “mango”, 3, 4 );

echo $x[0]; // bananaecho $x[1]; // orangeecho $x[3]; // 3

?>

Page 4: Class 4 - PHP Arrays

Ways to work with arrays

<?php$arr = array( “banana”, “orange”);

$arr[4] = “mango”;

$arr[] = “tomato”;

var_dump($arr); ?>

Page 5: Class 4 - PHP Arrays

Printing Arrays

• Use print_r() or var_dump() functions to output the contents of an array.

<?php$arr = array( “banana”, “orange”);

var_dump($arr ); // echoes the contents of the array

?>

Page 6: Class 4 - PHP Arrays

Enumerative VS. Associative

• Enumerative arrays are indexed using only numerical indexes.

<?php$arr = array( “banana”, “orange”);

echo $arr[0]; // banana?>

Page 7: Class 4 - PHP Arrays

Enumerative VS. Associative

• Associative arrays allow the association of an arbitrary key to every element.

<?php$arr = array( ‘first’ => “banana”,

‘second’ =>“orange”);

echo $arr[‘first’]; // banana?>

Page 8: Class 4 - PHP Arrays

Multidimensional Arrays

• Multidimensional arrays are arrays that contain other arrays.

<?php$arr = array(

array( ‘burger’, 5, 15 ),array( ‘cola’, 2, 25 ),array( ‘Juice’, 3, 7 ),

);

echo $arr[0][0]; // burger?>

Title Price Quantity

Burger 5 15

Cola 2 25

Juice 3 7

Page 9: Class 4 - PHP Arrays

Array Iteration

• foreach loop :Loop through arrays.

<?php$arr1 = array(“mango" , “banana" , “tomato”);

foreach( $arr1 as $key => $value ){echo $key . “ ----- > “ . $value . “<br/>”;

}?>

Page 10: Class 4 - PHP Arrays

Array Iteration

• We can use any other loop to go through arrays:

<?php$arr1 = array(“mango" , “banana" , “tomato”);

for( $i =0; $i < count($arr1) ; $i++ ){echo $i . “ ----- > “ . $arr1 [$i] . “<br/>”;

}?>

Page 11: Class 4 - PHP Arrays

Class Exercise

• Using arrays, write a PHP snippet that outputs the following table data in an HTML table and calculate the “total price” column values :

Title Price Quantity Total Price

Burger 5 10

Cola 2 4

Juice 3 7

Milk 2 6

Page 12: Class 4 - PHP Arrays

Class Exercise - Solution<?php

$array = array(

array( "name" => "Burger",

"price" => 5,

"quantity" => 10

),

array( "name" => "Cola",

"price" => 2,

"quantity" => 4

),

array( "name" => "Juice",

"price" => 3,

"quantity" => 7

),

array( "name" => "Milk",

"price" => 2,

"quantity" => 6

)

);

( Continued in the next slide )

Page 13: Class 4 - PHP Arrays

Class Exercise - Solution

echo '<table border="1">';

echo "<tr><th>Name</th><th>Price</th><th>Quantity</th><th>Total Price</th></tr>";

foreach( $array as $row ){

echo "<tr>";

echo "<td>" . $row['name'] . "</td>";

echo "<td>" . $row['price'] . "</td>";

echo "<td>" . $row['quantity'] . "</td>";

echo "<td>" . ( $row['price'] * $row['quantity'] ) . "</td>";

echo "</tr>";

}

echo "</table>";

?>

Page 14: Class 4 - PHP Arrays

Array Operations

• PHP has a vast number of functions that allow us to do many operations on arrays.

• For a complete reference of the functions, visit http://php.net/manual/en/ref.array.php.

Page 15: Class 4 - PHP Arrays

Array Comparison

• Equality ‘==‘:True if the keys and values are equal.<?php

$arr1 = array( “banana”, “mango”);$arr2 = array( “banana”, “tomato”);$arr3 = array( 1=> “mango”, 0=> “banana” );

if($arr1 == $arr2 ) // falseif($arr1 == $arr3 ) // true

?>

Page 16: Class 4 - PHP Arrays

Array Comparison

• Identical ‘===‘:True if the keys and values are equal and are in the same order.

<?php$arr1 = array( “banana”, “mango”);$arr3 = array( 1=> “mango”, 0=> “banana” );

if($arr1 === $arr3 ) // false?>

Page 17: Class 4 - PHP Arrays

Array Comparison

• array array_diff ( array $array1 , array $array2 [, array $ ... ] ) :

Returns an array containing all the entries from array1 that are not present in any of the other arrays.

<?php$arr1 = array( “banana”, “mango”, “lemon”);$arr2 = array( “banana”, “mango”);

$diff = array_diff($arr1, $arr2); // lemon?>

Page 18: Class 4 - PHP Arrays

Counting Arrays

• int count ( mixed $var [, int $mode = COUNT_NORMAL ] ) :Count all elements in an array.

<?php$arr1 = array( “banana”, “mango”, “lemon”);

echo count($arr1); // 3?>

Page 19: Class 4 - PHP Arrays

Searching Arrays

• mixed array_search ( mixed $needle , array $haystack [, bool $strict ] ) :Searches the array for a given value and returns the corresponding key if successful.

<?php$arr1 = array( “banana”, “mango”, “lemon”);

echo array_search( 'mango', $arr1 ); // 1echo array_search( ‘strawberry’, $arr1 ); // false

?>

Page 20: Class 4 - PHP Arrays

Deleting Items

• void unset ( mixed $var [, mixed $var [, mixed $... ]] ): Unset a given variable.

<?php$arr1 = array( “banana”, “mango”, “lemon”);

unset( $arr1[0] );

var_dump($arr1 ); // mango, lemon

?>

Page 21: Class 4 - PHP Arrays

Arrays Flipping

• array array_flip ( array $trans ) :Exchanges all keys with their associated values in an array

.

<?php$arr1 = array(“mango" => 1, “banana" => 2);$arr2 = array_flip($ arr1 );var_dump($ arr2 ); // 1 => mango, 2=> banana

?>

Page 22: Class 4 - PHP Arrays

Arrays Reversing

• array array_reverse ( array $array [, bool $preserve_keys = false ] ) :

Return an array with elements in reverse order.

<?php$arr1 = array(“mango" , “banana" , “tomato”);$arr2 = array_reverse($ arr1 );var_dump($ arr2 ); // tomato, banana, mango

?>

Page 23: Class 4 - PHP Arrays

Merging Arrays

• array array_merge ( array $array1 [, array $array2 [, array $... ]] )

Merges the elements of one or more arrays together so that the values of one are appended to the end of the previous one. It returns the resulting array.

<?php$array1 = array( 1, 2, 3 );

$array2 = array(4, 5, 6 );$result = array_merge($array1, $array2);print_r($result); // 1,2,3,4,5,6

?>

Page 24: Class 4 - PHP Arrays

Array Sorting

• bool sort ( array &$array [, int $sort_flags = SORT_REGULAR ] )

This function sorts an array. Elements will be arranged from lowest to highest when this function has completed.

<?php$fruits = array("lemon", "orange",

"banana", "apple");sort($fruits);var_dump($fruits); //apple, banana, lemon, orange

?>

Page 25: Class 4 - PHP Arrays

Array Sorting

• bool rsort ( array &$array [, int $sort_flags = SORT_REGULAR ] )This function sorts an array. Elements will be arranged from highest to lowest when this function has completed.

<?php$fruits = array("lemon", "orange",

"banana", "apple");rsort($fruits);var_dump($fruits); // orange, lemon, banana, apple

?>

Page 26: Class 4 - PHP Arrays

Array Sorting

• bool asort ( array &$array [, int $sort_flags = SORT_REGULAR ] )

Sorts an array from lowest to highest. This is used mainly when sorting associative arrays.

<?php$fruits = array( “one” => "lemon",

“two” => "orange", “three” => "banana", “four” => "apple");

asort($fruits);var_dump($fruits); // four => apple, three => banana, one => lemon,

two => orange

?>

Page 27: Class 4 - PHP Arrays

Array Sorting

• bool arsort ( array &$array [, int $sort_flags = SORT_REGULAR ] ) :

Sorts an array from highest to lowest. This is used mainly when sorting associative arrays.

<?php$fruits = array( “one” => "lemon",

“two” => "orange", “three” => "banana", “four” => "apple");

arsort($fruits);var_dump($fruits); // two => orange, one => lemon, three

=> banana, four => apple

?>

Page 28: Class 4 - PHP Arrays

Array Sorting

• bool ksort ( array &$array [, int $sort_flags = SORT_REGULAR ] )Sorts an array by key, maintaining key to data correlations. This is useful mainly for associative arrays.

<?php $fruits = array("d"=>"lemon",

"a"=>"orange", "b"=>"banana",

"c"=>"apple" );ksort($fruits);var_dump($fruits); // a => orange, b => banana, c => apple, d => lemon

?>

Page 29: Class 4 - PHP Arrays

Array Sorting

• bool krsort ( array &$array [, int $sort_flags = SORT_REGULAR ] ) Sorts an array by key in reverse order, maintaining key to data correlations. This is useful mainly for associative arrays.

<?php $fruits = array( "d"=>"lemon",

"a"=>"orange", "b"=>"banana",

"c"=>"apple" );krsort($fruits);var_dump($fruits); // d => lemon, c => apple, b => banana, a => orange

?>

Page 30: Class 4 - PHP Arrays

Assignment• Create a PHP function that takes an array as an argument and shows its

contents one on each line. This array may contain other arrays and these arrays may contain others, etc. The function should display all the values of these arrays. For example :

• If the array is like this :$array = array(

1, “Hello”,array( 2, 3 ,4),array(

5, array( 6, 7, 8)

),“No”

); ( Continued in the next slide )

Page 31: Class 4 - PHP Arrays

Assignment• The output should be like this :

1Hello2345678No

Page 32: Class 4 - PHP Arrays

What's Next?• Strings.

Page 33: Class 4 - PHP Arrays

Questions?