Home >Backend Development >PHP7 >What are PHP 7 Arrays and How Do I Work with Them?
PHP 7 arrays are actually ordered maps. This means they're not strictly arrays in the traditional computer science sense (contiguous blocks of memory holding elements of the same type). Instead, they're hash tables that associate keys with values. While you can use numerical indices (starting from 0), you can also use strings as keys. This flexibility is a key feature of PHP arrays.
Working with PHP 7 arrays involves several common operations:
Creating Arrays: You can create arrays using several methods:
$myArray = array('apple', 'banana', 'orange');
(Older syntax, still works)$myArray = ['apple', 'banana', 'orange'];
(Shorter, more modern syntax)$myArray = array("a" => "apple", "b" => "banana", "c" => "orange");
(Associative array using string keys)$myArray = [1 => "apple", 2 => "banana", 3 => "orange"];
(Associative array with numerical keys, but not necessarily sequential)Accessing Elements: You access elements using square brackets and the key:
echo $myArray[0];
// Outputs "apple" (for numerically indexed arrays)echo $myArray["a"];
// Outputs "apple" (for associative arrays)Adding Elements:
$myArray[] = "grape";
// Adds "grape" to the end of a numerically indexed array.$myArray["d"] = "grape";
// Adds "grape" with key "d" to an associative array.Modifying Elements:
$myArray[0] = "pear";
// Changes the first element.$myArray["a"] = "Pear";
// Changes the value associated with key "a".Checking for Keys: Use array_key_exists()
to see if a key exists before accessing it to avoid errors.
if (array_key_exists("z", $myArray)) { echo $myArray["z"]; }
Iterating through Arrays: Use foreach
loops:
foreach ($myArray as $value) { echo $value . "<br>"; }
// Iterates through values.foreach ($myArray as $key => $value) { echo "$key => $value<br>"; }
// Iterates through keys and values.While PHP 7 doesn't explicitly define different types of arrays like some languages (e.g., integer arrays, string arrays), the flexibility of using both numeric and string keys leads to different styles of arrays:
["name" => "John", "age" => 30, "city" => "New York"]
). The keys can be any string, not just alphanumeric characters; they can include spaces and other special characters.PHP 7 offers built-in functions for efficient searching and sorting:
Searching:
in_array($needle, $haystack)
: Checks if a value ($needle
) exists in an array ($haystack
). This is simple but can be slow for large arrays.array_search($needle, $haystack)
: Returns the key of the first occurrence of $needle
in $haystack
, or false
if not found.array_filter()
with a callback function.Sorting:
sort($array)
: Sorts an array in ascending order (preserving keys for numerically indexed arrays, but re-indexing for associative arrays).rsort($array)
: Sorts in descending order.asort($array)
: Sorts an associative array in ascending order by value, preserving keys.arsort($array)
: Sorts an associative array in descending order by value, preserving keys.ksort($array)
: Sorts an associative array in ascending order by key.krsort($array)
: Sorts an associative array in descending order by key.usort($array, $comparison_function)
: Sorts using a custom comparison function, providing maximum flexibility.array_filter()
, array_map()
, and array_reduce()
for common array operations instead of writing custom loops. These functions are often optimized.undefined index
errors. Use isset()
or array_key_exists()
for this purpose.By following these best practices, you can write efficient and maintainable PHP code that effectively utilizes arrays. Remember to profile your code to identify performance bottlenecks if necessary.
The above is the detailed content of What are PHP 7 Arrays and How Do I Work with Them?. For more information, please follow other related articles on the PHP Chinese website!