class 4 - php arrays

Post on 16-Jan-2017

484 Views

Category:

Technology

13 Downloads

Preview:

Click to see full reader

TRANSCRIPT

PHP Arrays

Outline

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

?>

Ways to work with arrays

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

$arr[4] = “mango”;

$arr[] = “tomato”;

var_dump($arr); ?>

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

?>

Enumerative VS. Associative

• Enumerative arrays are indexed using only numerical indexes.

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

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

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?>

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

Array Iteration

• foreach loop :Loop through arrays.

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

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

}?>

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/>”;

}?>

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

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 )

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>";

?>

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.

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

?>

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?>

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?>

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?>

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

?>

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

?>

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

?>

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

?>

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

?>

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

?>

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

?>

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

?>

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

?>

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

?>

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

?>

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 )

Assignment• The output should be like this :

1Hello2345678No

What's Next?• Strings.

Questions?

top related