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: Hàm array_values() trong php
Numeric Arrays
Numeric arrays use number as access keys.An access key is a reference khổng lồ a memory slot in an array variable.The access key is used whenever we want khổng lồ read or assign a new value an array element.Below is the syntax for creating numeric array in php. Array ExampleOr value, …);?>HERE, “$variable_name…” is the name of the variable “ Xem thêm: Cung Cấp Hóa Chất Gelatin Trong Thực Phẩm Là Gì? Ứng Dụng Trong Đời Sống CủaPHP 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 the array element.Let’s suppose that we have a group of persons, and we want khổng lồ assign the gender of each person against their names.We can use an associative array to vày that.The code below helps us to vày that. "Female", "John" => "Male", "Mirriam" => "Female");print_r($persons); echo ""; echo "Mary is a " . $persons<"Mary">;?> HERE,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 menu of movies by category.
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 & Juliet"));print_r($movies);?>HERE,Movie titleCategory Pink Panther Comedy John English Comedy Die Hard Action Expendables Action The Lord of the rings Epic Romeo and Juliet Romance See no evil hear no evil Comedy PHP Arrays: Operators
OperatorNameDescriptionHow to vị itOutput x + y Union Combines elements from both arrays 1);$y = array("value" => 10);$z = $x + $y;?> Array( X == y Equal Compares 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 === y Identical Compares both the values và data types 1);$y = array("id" => "1");if($x === $y)echo "true";elseecho "false";?> False or 0 X != y, x y Not equal 1);$y = array("id" => "1");if($x != $y)echo "true";elseecho "false";?> False or 0 X !== y Non 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 khổng lồ count the number of elements that an php array contains. The code below shows the implementation.Output:3is_array function
The is_array function is used to determine if a variable is an array or not. Let’s now look at an example that implements the is_array functions.Output:1Sort
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ỉ 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: