PHP Arrays Work
PHP Arrays Work
1. Introduction
2. Creating an array
3. Updating an array
4. Removing items from an array
5. Sorting an array
1.INTRODUCTION
What is an array?
An array is a special variable that stores many values under a single name, and you can access the
values by referring to an index number or name.
2.Creating an Array
Array Declaration
Example:
<?php
$names = array(“iraasubiza” , ”saly” , ”nelson” );
?>
TYPES OF ARRAYS
Associative arrays are arrays that use named keys that you
assign to them.
Example: <?php
$student = array(“gender"=>“male", “age"=>10, “school"=>”RCA”);
?>
3.MULTIDIMENSIONAL ARRAY:
A multidimensional array is an array containing one or more arrays. It can be one dimensional as well
as two dimensional array.
Example: <?php
$cars = array (
array("Volvo",22,18),
array("BMW",15,13),
array("Saab",5,2),
array("Land Rover",17,15)
);
ACCESSING ARRAY ITEMS
To access an array item, you can refer to the index number for indexed arrays, and the
key name for associative arrays.
Example:
//for indexed arrays
<?php
$names = array(“irasubiza", “saly", “nelson");
echo $names[2]; =>it will output Toyota
<?php
$cars = array (
array("Volvo",22,18),
array("BMW",15,13),
array("Saab",5,2),
array("Land Rover",17,15)
);
ACCESSING ITEMS IN MULTIDIMENSIONAL ARRAY
To update an existing array item, you can refer to the index number for indexed
arrays, and the key name for associative arrays.
Example:
<?php
$cars = array("BMW", "Volvo", "Toyota");
$cars[1] = "Ford"; // value of Volvo is updated to Ford
$student = array(“gender"=>“male", “age"=>10, “school"=>”RCA”);
$student[“school"] = “LNDC”; // school is
changed to LNDC
DELETE ARRAY ITEMS
• To remove an existing item from an array, you can use the unset()
function
• Example: <?php
• $cars = array("Volvo", "BMW", "Toyota");
• unset($cars[0], $cars[1]); // there is only Toyota in the cars array
• To remove items from an associative array, you can use unset() function
like before, but referring to the key name instead of index.
SORTING ARRAYS
• <?php
• $numbers = array(4, 6, 2, 22, 11);
• sort($numbers);
• Etc.…
ARRAY FUNCTIONS
array_diff_ukey(): Compare arrays, and returns the differences (compare keys only, using a
user-defined key comparison function).
array_fill(): Fills an array with values.
array_fill_keys(): Fills an array with values, specifying keys.
array_filter(): Filters the values of an array using a callback function.
array_flip(): Flips/Exchanges all keys with their associated values in an array.
array_intersect(): Compare arrays, and returns the matches (compare values only).
array_intersect_assoc(): Compare arrays and returns the matches (compare keys and values).
the reverse order.
ARRAY FUNCTIONS
array_combine(): Creates an array by using the elements from one “keys” array and one
“values” array.
array_count_values(): Counts all the values of an array.
array_diff(): Compare arrays, and returns the differences (compare values only).
array_diff_assoc(): Compare arrays, and returns the differences (compare keys and values).
array_diff_key(): Compare arrays, and returns the differences (compare keys only).
array_diff_uassoc(): Compare arrays, and returns the differences (compare keys and values,
using a user-defined key comparison function).
ARRAY FUNCTIONS
array_map(): Sends each value of an array to a user-made function, which returns new
values.
array_merge(): Merges one or more arrays into one array.
array_merge_recursive(): Merges one or more arrays into one array recursively.
array_multisort(): Sorts multiple or multi-dimensional arrays.
ARRAY FUNCTIONS
:$cars[]=“Daihatsu” ;
$cars[1]=“vigo”;
echo $cars[1] // output will be vigo
b)For associative arrays
$cars[vips]=“Mercedes”;
echo $cars[vips]; //output will be Mercedes
THANK YOU ,!!!!!!!!!!!!