How to expert in PHP array
An array in PHP is a data structure that allows you to store multiple values in a single variable. Arrays can hold different types of data (numbers, strings, objects, etc.), and they can be indexed by numbers (numerically indexed arrays) or by strings (associative arrays).
Types of Arrays in PHP:
1. Indexed Array:
- An array with a numeric index.
PHP
$fruits = array("Apple", "Banana", "Cherry"); // or using the short syntax $fruits = ["Apple", "Banana", "Cherry"]; echo $fruits[0];
PHP Output
Apple
2. Associative Array:
- An array where the keys are strings.
PHP
$person = array("name" => "Raghu", "age" => 25, "city" => "New York"); // or using the short syntax $person = ["name" => "John", "age" => 25, "city" => "New York"]; echo $person['name'];
PHP Output
Raghu
3. Multidimensional Array:
- An array containing one or more arrays.
PHP
$people = array( array("name" => "Superman", "age" => 25), array("name" => "Jane", "age" => 28) ); echo $people[0]['name'];
PHP Output
Superman
Useful Array Functions in PHP:
count($array)
: Returns the number of elements in an array.array_merge($array1, $array2)
: Merges two or more arrays.in_array($needle, $haystack)
: Checks if a value exists in an array.array_keys($array)
: Returns all the keys of an array.array_values($array)
: Returns all the values of an array.
Example
$numbers = [1, 2, 3, 4, 5]; $sum = array_sum($numbers); echo $sum;
PHP Output
15
Arrays in PHP are quite versatile, and you can easily manipulate them with built-in functions.
Popular post
-
Eclipse IDE – Create New Java Project.
Opening the New Java Project…
-
How to start the project in android studio
Android Studio Open the Android…
-
How to use ACOSH function in excel
The ACOSH function returns the…
-
Complete Header tags in html – easy to learn
H tags can be used…
-
Best features in Python programme – easy to learn
Python is the most widely…