PEMROGRAMAN BERBASIS WEB UNIVERSITAS TEKNOKRAT INDONESIA FAKULTAS TEKNIK DAN ILMU KOMPUTER YUSRA FERNANDO, S.KOM., M.KOM by: 2020 Web Base Programming 1
PHP UNIVERSITAS TEKNOKRAT INDONESIA FAKULTAS TEKNIK DAN ILMU KOMPUTER 2020 PHP: Hypertext Preprocessor server side scripting 2
The PHP Array UNIVERSITAS TEKNOKRAT INDONESIA What is an Array? “An array is a special variable, which can hold more than one value at a time.” $cars1 = "Volvo"; $cars2 = "BMW"; $cars3 = "Toyota"; If you have a list of items (a list of car names, for example), storing the cars in single variables could look like this:
The PHP Array UNIVERSITAS TEKNOKRAT INDONESIA An array in PHP is actually an ordered map. A map is a type that associates values to keys. This type is optimized for several different uses; it can be treated as an array, list (vector), hash table (an implementation of a map), dictionary, collection, stack, queue, and probably more. As array values can be other arrays, trees and multidimensional arrays are also possible. Explanation of those data structures is beyond the scope of this manual, but at least one example is provided for each of them. For more information, look towards the considerable literature that exists about this broad topic.
Sintax UNIVERSITAS TEKNOKRAT INDONESIA Specifying with array() An array can be created using the array() language construct. It takes any number of comma-separated key => value pairs as arguments. array( key => value, key2 => value2, key3 => value3, ... ) The comma after the last array element is optional and can be omitted. This is usually done for single-line arrays, i.e. array(1, 2) is preferred over array(1, 2, ). For multi-line arrays on the other hand the trailing comma is commonly used, as it allows easier addition of new elements at the end.
Create an Array in PHP UNIVERSITAS TEKNOKRAT INDON In PHP, the array() function is used to create an array: array(); In PHP, there are three types of arrays: • Indexed arrays - Arrays with a numeric index • Associative arrays - Arrays with named keys • Multidimensional arrays - Arrays containing one or more arrays
Get The Length of an Array - The count() Function UNIVERSITAS TEKNOKRAT INDONESIA The count() function is used to return the length (the number of elements) of an array: Example <?php $cars = array("Volvo", "BMW", "Toyota"); echo count($cars); ?>
Example #1 A simple array UNIVERSITAS TEKNOKRAT INDONESIA <?php $array = array( "foo" => "bar", "bar" => "foo", ); // as of PHP 5.4 $array = [ “foo" => "bar", "bar" => "foo", ]; ?>
Example #2 Type Casting and Overwriting example UNIVERSITAS TEKNOKRAT INDONESIA <?php $array = array( 1 => "a", "1" => "b", 1.5 => "c", true => "d", ); var_dump($array); ?>
Example #3 Mixed integer and string keys UNIVERSITAS TEKNOKRAT INDONESIA <?php $array = array( "foo" => "bar", "bar" => "foo", 100 => -100, -100 => 100, ); var_dump($array); ?>
Example #4 Indexed arrays without key UNIVERSITAS TEKNOKRAT INDONESIA <?php $array = array("foo", "bar", "hello", "world"); var_dump($array); ?>
Example #5 Keys not on all elements UNIVERSITAS TEKNOKRAT INDONESIA <?php $array = array( "a", "b", 6 => "c", "d", ); var_dump($array); ?>
TUGAS PHP ARRAY UNIVERSITAS TEKNOKRAT INDONESIA Tulis ulang script berikut, File: (array1.php) Task 1
TUGAS PHP ARRAY UNIVERSITAS TEKNOKRAT INDONESIA Tulis ulang script berikut, File: (array2.php) Task 2
TUGAS PHP ARRAY UNIVERSITAS TEKNOKRAT INDONESIA Tulis ulang script berikut , File: (array3.php) Task 2
TERIMAKASIH ... UNIVERSITAS TEKNOKRAT INDONESIA FAKULTAS TEKNIK DAN ILMU KOMPUTER YUSRA FERNANDO, S.KOM., M.KOM oleh : 2020 16

8 covid19 - chp 07 - php array (shared)

  • 1.
    PEMROGRAMAN BERBASIS WEB UNIVERSITASTEKNOKRAT INDONESIA FAKULTAS TEKNIK DAN ILMU KOMPUTER YUSRA FERNANDO, S.KOM., M.KOM by: 2020 Web Base Programming 1
  • 2.
    PHP UNIVERSITAS TEKNOKRAT INDONESIA FAKULTASTEKNIK DAN ILMU KOMPUTER 2020 PHP: Hypertext Preprocessor server side scripting 2
  • 3.
    The PHP Array UNIVERSITAS TEKNOKRATINDONESIA What is an Array? “An array is a special variable, which can hold more than one value at a time.” $cars1 = "Volvo"; $cars2 = "BMW"; $cars3 = "Toyota"; If you have a list of items (a list of car names, for example), storing the cars in single variables could look like this:
  • 4.
    The PHP Array UNIVERSITAS TEKNOKRATINDONESIA An array in PHP is actually an ordered map. A map is a type that associates values to keys. This type is optimized for several different uses; it can be treated as an array, list (vector), hash table (an implementation of a map), dictionary, collection, stack, queue, and probably more. As array values can be other arrays, trees and multidimensional arrays are also possible. Explanation of those data structures is beyond the scope of this manual, but at least one example is provided for each of them. For more information, look towards the considerable literature that exists about this broad topic.
  • 5.
    Sintax UNIVERSITAS TEKNOKRAT INDONESIA Specifying witharray() An array can be created using the array() language construct. It takes any number of comma-separated key => value pairs as arguments. array( key => value, key2 => value2, key3 => value3, ... ) The comma after the last array element is optional and can be omitted. This is usually done for single-line arrays, i.e. array(1, 2) is preferred over array(1, 2, ). For multi-line arrays on the other hand the trailing comma is commonly used, as it allows easier addition of new elements at the end.
  • 6.
    Create an Arrayin PHP UNIVERSITAS TEKNOKRAT INDON In PHP, the array() function is used to create an array: array(); In PHP, there are three types of arrays: • Indexed arrays - Arrays with a numeric index • Associative arrays - Arrays with named keys • Multidimensional arrays - Arrays containing one or more arrays
  • 7.
    Get The Lengthof an Array - The count() Function UNIVERSITAS TEKNOKRAT INDONESIA The count() function is used to return the length (the number of elements) of an array: Example <?php $cars = array("Volvo", "BMW", "Toyota"); echo count($cars); ?>
  • 8.
    Example #1 Asimple array UNIVERSITAS TEKNOKRAT INDONESIA <?php $array = array( "foo" => "bar", "bar" => "foo", ); // as of PHP 5.4 $array = [ “foo" => "bar", "bar" => "foo", ]; ?>
  • 9.
    Example #2 TypeCasting and Overwriting example UNIVERSITAS TEKNOKRAT INDONESIA <?php $array = array( 1 => "a", "1" => "b", 1.5 => "c", true => "d", ); var_dump($array); ?>
  • 10.
    Example #3 Mixedinteger and string keys UNIVERSITAS TEKNOKRAT INDONESIA <?php $array = array( "foo" => "bar", "bar" => "foo", 100 => -100, -100 => 100, ); var_dump($array); ?>
  • 11.
    Example #4 Indexedarrays without key UNIVERSITAS TEKNOKRAT INDONESIA <?php $array = array("foo", "bar", "hello", "world"); var_dump($array); ?>
  • 12.
    Example #5 Keysnot on all elements UNIVERSITAS TEKNOKRAT INDONESIA <?php $array = array( "a", "b", 6 => "c", "d", ); var_dump($array); ?>
  • 13.
    TUGAS PHP ARRAY UNIVERSITAS TEKNOKRATINDONESIA Tulis ulang script berikut, File: (array1.php) Task 1
  • 14.
    TUGAS PHP ARRAY UNIVERSITAS TEKNOKRATINDONESIA Tulis ulang script berikut, File: (array2.php) Task 2
  • 15.
    TUGAS PHP ARRAY UNIVERSITAS TEKNOKRATINDONESIA Tulis ulang script berikut , File: (array3.php) Task 2
  • 16.
    TERIMAKASIH ... UNIVERSITAS TEKNOKRATINDONESIA FAKULTAS TEKNIK DAN ILMU KOMPUTER YUSRA FERNANDO, S.KOM., M.KOM oleh : 2020 16