PHP Arrays

In PHP, an array is a data structure that allows you to store multiple values in a single variable. Arrays can hold different types of data, such as integers, strings, and other arrays. Each value in an array is assigned a unique index, starting from zero.

There are three types of arrays in PHP:

  1. Indexed arrays
  2. Associative arrays
  3. Multidimensional array

1. Indexed arrays

In PHP, an indexed array is an array where each value is assigned a numeric index starting from 0. Indexed arrays are the simplest type of array and are used to store a list of values.

Here’s an example of an indexed array in PHP:

$fruits = array("apple", "banana", "orange", "mango");

In the above example, we have defined an array called $fruits that contains four elements, each assigned an index starting from 0.

You can access the values of an indexed array by using its index. Here’s an example of how to access the values of an indexed array:

$fruits = array("apple", "banana", "orange", "mango");
echo $fruits[0]; // output: apple
echo $fruits[1]; // output: banana

In the above code, we have used the square brackets to access the value of the first and second elements in the $fruits array.

You can also modify the values of an indexed array by assigning a new value to its index. Here’s an example of how to modify the values of an indexed array:

$fruits = array("apple", "banana", "orange", "mango");
$fruits[0] = "pear";
$fruits[1] = "kiwi";
print_r($fruits); // output: Array ( [0] => pear [1] => kiwi [2] => orange [3] => mango )

In the above code, we have modified the values of the first and second elements in the $fruits array, and then printed the array using the print_r() function.

2. Associative arrays

In PHP, an associative array is an array where each value is assigned a unique key, instead of a numeric index starting from 0. Associative arrays are used to store key-value pairs, where the keys are used to access the corresponding values.

Here’s an example of an associative array in PHP:

$person = array("name" => "John", "age" => 30, "city" => "New York");

In the above example, we have defined an array called $person that contains three key-value pairs. The keys are “name”, “age”, and “city”, and their corresponding values are “John”, 30, and “New York”, respectively.

You can access the values of an associative array by using its key. Here’s an example of how to access the values of an associative array:

$person = array("name" => "John", "age" => 30, "city" => "New York");
echo $person["name"]; // output: John
echo $person["age"]; // output: 30

In the above code, we have used the square brackets to access the values of the “name” and “age” keys in the $person array.

You can also modify the values of an associative array by assigning a new value to its key. Here’s an example of how to modify the values of an associative array:

$person = array("name" => "John", "age" => 30, "city" => "New York");
$person["name"] = "Mike";
$person["age"] = 25;
print_r($person); // output: Array ( [name] => Mike [age] => 25 [city] => New York )

In the above code, we have modified the values of the “name” and “age” keys in the $person array, and then printed the array using the print_r() function.

3. Multidimensional arrays

In PHP, a multidimensional array is an array that contains one or more arrays as its elements. In other words, each element in a multidimensional array can be an array itself, forming a hierarchy of arrays.

Multidimensional arrays are useful when you need to store data in a structured format, such as a table or a grid. For example, you could use a multidimensional array to store the results of a quiz, with each row representing a student and each column representing a question.

Here’s an example of a multidimensional array in PHP:

$quizResults = array(
    array("John", 10, 8, 9),
    array("Jane", 9, 7, 10),
    array("Mike", 7, 9, 8)
);

In the above example, we have defined a multidimensional array called $quizResults. The array contains three sub-arrays, each representing the quiz results of a student. Each sub-array contains four elements: the student’s name, and their scores for three questions.

You can access the elements of a multidimensional array by using multiple sets of square brackets, one for each level of the hierarchy. Here’s an example of how to access the elements of the $quizResults array:

echo $quizResults[0][0]; // output: John
echo $quizResults[1][2]; // output: 10

In the above code, we have used two sets of square brackets to access the elements of the $quizResults array. The first set of brackets selects the sub-array, and the second set of brackets selects the element within the sub-array.

You can also modify the elements of a multidimensional array by assigning a new value to its index. Here’s an example of how to modify the elements of the $quizResults array:

$quizResults[2][1] = 10;
print_r($quizResults); // output: Array ( [0] => Array ( [0] => John [1] => 10 [2] => 9 [3] => 8 ) [1] => Array ( [0] => Jane [1] => 9 [2] => 7 [3] => 10 ) [2] => Array ( [0] => Mike [1] => 10 [2] => 9 [3] => 8 ) )

In the above code, we have modified the score of the second question for the third student, and then printed the $quizResults array using the print_r() function.

Wordpress Social Share Plugin powered by Ultimatelysocial
Wordpress Social Share Plugin powered by Ultimatelysocial