In php three types of arrays are

Advertisement Remove all ads

Advertisement Remove all ads

Fill in the Blanks

ln PHP, three types of arrays are ________, _________, __________.

a) Indexed

b) Simple

c) Associative

d) Multidimensional

e) Complex

f) General

Advertisement Remove all ads

Solution

ln PHP, three types of arrays are Indexed, Associative, Multidimensional.

Concept: PHP Arrays - Indexed Arrays

  Is there an error in this question or solution?

Advertisement Remove all ads

Chapter 5: Server-Side Scripting (PHP) - Exercises [Page 78]

Q 5.1Q 4.2Q 5.2

APPEARS IN

Balbharati Information Technology (IT) (Science) 12th Standard HSC Maharashtra State Board

Chapter 5 Server-Side Scripting (PHP)
Exercises | Q 5.1 | Page 78

Advertisement Remove all ads

In this tutorial you'll learn how to store multiple values in a single variable in PHP.

What is PHP Arrays

Arrays are complex variables that allow us to store more than one value or a group of values under a single variable name. Let's suppose you want to store colors in your PHP script. Storing the colors one by one in a variable could look something like this:

<?php
$color1 = "Red";
$color2 = "Green";
$color3 = "Blue";
?>

But what, if you want to store the states or city names of a country in variables and this time this not just three may be hundred. It is quite hard, boring, and bad idea to store each city name in a separate variable. And here array comes into play.


Types of Arrays in PHP

There are three types of arrays that you can create. These are:

  • Indexed array — An array with a numeric key.
  • Associative array — An array where each key has its own specific value.
  • Multidimensional array — An array containing one or more arrays within itself.

Indexed Arrays

An indexed or numeric array stores each array element with a numeric index. The following examples shows two ways of creating an indexed array, the easiest way is:

<?php
// Define an indexed array
$colors = array("Red", "Green", "Blue");
?>

Note: In an indexed or numeric array, the indexes are automatically assigned and start with 0, and the values can be any data type.

This is equivalent to the following example, in which indexes are assigned manually:

<?php
$colors[0] = "Red"; 
$colors[1] = "Green"; 
$colors[2] = "Blue"; 
?>


Associative Arrays

In an associative array, the keys assigned to values can be arbitrary and user defined strings. In the following example the array uses keys instead of index numbers:

<?php
// Define an associative array
$ages = array("Peter"=>22, "Clark"=>32, "John"=>28);
?>

The following example is equivalent to the previous example, but shows a different way of creating associative arrays:

<?php
$ages["Peter"] = "22";
$ages["Clark"] = "32";
$ages["John"] = "28";
?>


Multidimensional Arrays

The multidimensional array is an array in which each element can also be an array and each element in the sub-array can be an array or further contain array within itself and so on. An example of a multidimensional array will look something like this:

<?php
// Define a multidimensional array
$contacts = array(
    array(
        "name" => "Peter Parker",
        "email" => "",
    ),
    array(
        "name" => "Clark Kent",
        "email" => "",
    ),
    array(
        "name" => "Harry Potter",
        "email" => "",
    )
);
// Access nested value
echo "Peter Parker's Email-id is: " . $contacts[0]["email"];
?>


Viewing Array Structure and Values

You can see the structure and values of any array by using one of two statements — var_dump() or print_r(). The print_r() statement, however, gives somewhat less information. Consider the following example:

<?php
// Define array
$cities = array("London", "Paris", "New York");
 
// Display the cities array
print_r($cities);
?>

The print_r() statement gives the following output:

Array ( [0] => London [1] => Paris [2] => New York )

This output shows the key and the value for each element in the array. To get more information, use the following statement:

<?php
// Define array
$cities = array("London", "Paris", "New York");
 
// Display the cities array
var_dump($cities);
?>

This var_dump() statement gives the following output:

array(3) { [0]=> string(6) "London" [1]=> string(5) "Paris" [2]=> string(8) "New York" }

This output shows the data type of each element, such as a string of 6 characters, in addition to the key and value. In the next chapter you will learn how to sort array elements.

You will learn how to loop through the values of an array in the later chapter.

What is array in PHP with example?

More Detail. An array is a data structure that stores one or more similar type of values in a single value. For example if you want to store 100 numbers then instead of defining 100 variables its easy to define an array of 100 length.

What is numeric array in PHP?

Numeric arrays allow us to store multiple values of the same data type in a single variable without having to create separate variables for each value. These values can then be accessed using an index which in case of numeric arrays is always a number. Note: By default the index always starts at zero.

What are Indexed array and associative arrays?

An array is a type of variable that may contain several values at once. There are three types of arrays, namely: Indexed array - An array with a numeric key. Associative array — An array where each key has its own specific value. Multidimensional array — An array containing one or more arrays within itself.

Which of the following is not a type of array in PHP?

Q.
In PHP, there are three types of arrays one is not type of array in phpwhich one:
B.
Associative arrays -
C.
Multidimensional arrays -
D.
polygamy array
Answer» d. polygamy array
[Solved] In PHP, there are three types of arrays one is not type ... - McqMatemcqmate.com › discussion › in-php-there-are-three-types-of-arrays-one-is-...null