Php array in php w3, php array: associative, multidimensional

What is a PHP Array?

A PHP array is a variable that stores more than one piece of related data in a single variable.Think of an array as a box of chocolates with slots inside.The box represents the array itself while the spaces containing chocolates represent the values stored in the arrays.The diagram below illustrates the above syntax.

Bạn đang xem: Php array in php w3, php array: associative, multidimensional

Numeric Arrays

Numeric arrays use number as access keys.An access key is a reference to lớn a memory slot in an array variable.The access key is used whenever we want lớn read or assign a new value an array element.Below is the syntax for creating numeric array in php. Array Example

Or value, …);?>HERE, “$variable_name…” is the name of the variable “” is the access index number of the element “value” is the value assigned lớn the array element.Let’s now look at an example of a numeric array.Suppose we have 5 movies that we want khổng lồ store in array variables.We can use the example shown below to vị that.Here,

*
Each movie is given an index number that is used to retrieve or modify its value. Observe the following code-Output:Once upon a time in trung quốc Eastern CondorsAs you can see from the above examples, working with arrays in PHP when dealing with multiple values of the same nature is very easy và flexible.Alternatively, the above array variables can also be created using the following code. "Shaolin Monk", 1 => "Drunken Master", 2 => "American Ninja", 3 => "Once upon a time in China", 4 =>"Replacement Killers" );echo $movie<4>;?>Output:Replacement Killers

PHP Associative Array

Associative array differ from numeric array in the sense that associative arrays use descriptive names for id keys.Below is the syntax for creating associative array in php. Value);?>HERE, “$variable_name…” is the name of the variable “<"key_name">” is the access index number of the element “value” is the value assigned to lớn the array element.Let’s suppose that we have a group of persons, và we want khổng lồ assign the gender of each person against their names.We can use an associative array to vì that.The code below helps us to bởi that. "Female", "John" => "Male", "Mirriam" => "Female");print_r($persons); echo ""; echo "Mary is a " . $persons<"Mary">;?> HERE,
*
Output:Array ( => Female => Male => Female ) Mary is a FemaleAssociative array are also very useful when retrieving data from the database.The field names are used as id keys.

PHP Multi-dimensional arrays

These are arrays that contain other nested arrays.The advantage of multidimensional arrays is that they allow us khổng lồ group related data together.Let’s now look at a practical example that implements a php multidimensional array.The table below shows a các mục of movies by category.

Xem thêm: Ung Thư U Xơ Tử Cung - U Xơ Tử Cung Có Gây Ung Thư Không

Movie titleCategory
Pink PantherComedy
John EnglishComedy
Die HardAction
ExpendablesAction
The Lord of the ringsEpic
Romeo and JulietRomance
See no evil hear no evilComedy
The above information can be represented as a multidimensional array. The code below shows the implementation. Array("Pink Panther", "John English", "See no evil hear no evil"),"action" => array("Die Hard", "Expendables"),"epic" => array("The Lord of the rings"),"Romance" => array("Romeo và Juliet"));print_r($movies);?>HERE,
*
Output:Array ( => Array ( <0> => Pink Panther <1> => John English <2> => See no evil hear no evil ) => Array ( <0> => Die Hard <1> => Expendables ) => Array ( <0> => The Lord of the rings ) => Array ( <0> => Romeo & Juliet ) )Another way lớn define the same array is as follows array( 0 => "Pink Panther", 1 => "john English", 2 => "See no evil hear no evil" ), "action" => array ( 0 => "Die Hard", 1 => "Expendables" ), "epic" => array ( 0 => "The Lord of the rings" ), "Romance" => array ( 0 => "Romeo and Juliet" ));echo $film<"comedy"><0>;?>Output:Pink PantherNote: the movies numeric array has been nested inside the categories associative array

PHP Arrays: Operators

OperatorNameDescriptionHow to vì chưng itOutput
x + yUnionCombines elements from both arrays 1);$y = array("value" => 10);$z = $x + $y;?>Array( => 1 => 10)
X == yEqualCompares two arrays if they are equal và returns true if yes. 1);$y = array("id" => "1");if($x == $y)echo "true";elseecho "false";?>True or 1
X === yIdenticalCompares both the values and data types 1);$y = array("id" => "1");if($x === $y)echo "true";elseecho "false";?>False or 0
X != y, x yNot equal 1);$y = array("id" => "1");if($x != $y)echo "true";elseecho "false";?>False or 0
X !== yNon identical 1);$y = array("id" => "1");if($x !== $y)echo "true";elseecho "false";?>True or 1

PHP Array Functions

Count function

The count function is used to count the number of elements that an php array contains. The code below shows the implementation.Output:3

is_array function

The is_array function is used lớn determine if a variable is an array or not. Let’s now look at an example that implements the is_array functions.Output:1

Sort

This function is used to lớn sort arrays by the values.If the values are alphanumeric, it sorts them in alphabetical order.If the values are numeric, it sorts them in ascending order.It removes the existing access keys and địa chỉ cửa hàng new numeric keys.The output đầu ra of this function is a numeric array "Female", "John" => "Male", "Mirriam" => "Female");sort($persons);print_r($persons);?>Output: