Deva Point

deva point

Our Latest Programming Tutorial

Easy To Learning

Array in PHP

An array contains multiple values in a single variable:

Example

The $cars array is an array(“Bolero”, “Alto”, “Safari”);

echo “I like ” . $cars[0] . “, ” . $cars[1] . ” and ” . $cars[2] . “. “;

?>

What is an Array in PHP?

An array in PHP refers to a variable that holds more than one item of information in one variable. The box represents the array and the chocolate-filled spaces represent the contents of the arrays. If you’ve got a list of things (a list of the names of cars for instance) and you want to store the cars in one variable might be like this:

$cars1 = “Bolero”;

$cars2 = “Alto”;

$cars3 = “Safari”;

But, what happens if you’re looking to go through the cars in order to locate one specific car? What if you had not three cars and 300?

Why Use PHP Arrray?

  • There is no have for multiple variables to be defined.
  • The contents of arrays can be stretched.
  • Arrays can be used to help group related details such as login details.
  • Numeric arrays make use of numbers to represent the array’s key.
  • PHP Associative array use descriptive names for array keys.
  • Multidimensional arrays include different arrays within them.
  • Simple to traverse: With using a single loops, we can traverse all elements of an array.
  • Sorting The ability to sort elements of an array.
  • Arrays assist in writing better code. The answer is to make an array.

An array could contain a variety of different values in a single name. It is possible to access the array’s contents through the index numbers.

Create an Array in PHP

In PHP using the array() function is used to create an array.

There are Three Types of Arrays in PHP

  • Numeric Arrays, also known as Indexed arrays – Arrays that have a numeric index
  • Associative arrays – Arrays that have named keys
  • Multidimensional arrays- Arrays made up of an array or arrays

A: Numeric arrays or Indexed arrays

Arrays may be used to store strings, numbers and other objects, but their index is expressed as numbers. The index of arrays is set to start with zero. While ordered arrays share identical to associative arrays, it is recommended to treat both independently.

Example 1:

Here example shows how to build and access numeric arrays. In this example, we’ve used the array() function to generate an array. This function is explained in the function reference.

Program

* First method of creating array. */

The array of $numbers is( 1 2, 3, 4 5,);

foreach( $numbers as value ) {*

echo “Value is $value” echo “Value

“;

}

* Second method to make array. *

$numbers[0] = “one”;

$numbers[1] = “two”;

$numbers[2] = “three”;

$numbers[3] = “four”;

$numbers[4] = “five”;

foreach( $numbers as the value ) {foreach( $value as $numbers)

echo “Value is $value” echo “Value

“;

}

?>

The following outcome -Value is 1

Value is 2

Value is 3

Value is 4

Value is 5

Value is one

Value is two

Three times the value of three

Value is four

Value is five

B: Associative Arrays

These kinds of arrays are comparable to indexable arrays, however rather than linearly storing every value is assigned by a user-defined number of the string type.

Example: 2

How to Make an Associative Array

$name_one = array(“Mark”=>”Jaklin”, “Martin”=>”Jasmin”,

“Ganga”=>”Roman”, “Maynk”=>”Shekher”,

“Rajan “=>”Rehan”);

/Accessing elements directly

Echo “Accessing the elements directly:\n”;

echo $name_two[“Jaklin”], “\n”;

echo $name_two[“Jasmin”], “\n”;

echo $name_two[“Roman”], “\n”;

echo $name_one[“Shekher”], “\n”;

echo $name_one[“Rehan”], “\n”;

?>

Output:

Accessing elements directly:

Mark

Martin

Ganga

Mayank

Rajan

C: Multidimensional Arrays

A multi-dimensional array where each element of the main array could also constitute an array. Every element in the sub-array could be an array or array of elements, and list goes on. The array’s values can be accessed by multiple index.

Example 3:

In this case, we construct an array of two dimensions to record the marks of three students across three different subjects. This is an example of an associative array. You can also create numeric arrays similar to this.

Program

The array is $marks.(

“Jasmin” => array (

“Hindi” => 35,

“English” => 30,

“Math” => 39

),

“Martin” => array (

“Hnidi” => 30,

“English” => 32,

“Math” => 29

),

“Rehan” => array (

“Hindi” => 31,

“Engilsh” => 22,

“Math” => 39

)

);

accessing arrays with multidimensional values/

Echo “Marks for Jasmin in Hindi : ” ;

echo $marks[‘Jasmin’][‘Hindi’] . “

“;

“echoes “Marks for Martin in Engilsh : “;

echo $marks[‘Martin’][‘Enlish’] . “

“;

“echoes “Marks for Rehan in Math : ” ;

echo $marks[‘Rehan’][‘Math’] . “

“;

?>

The following outcome –

Marks for Jasmin in Hindi : 35

Marks for Martin in English : 32

Marks for Rehan in Math : 39

Published
Categorized as PHP

Leave a comment

Your email address will not be published.