Fixed various grammatical issues. (#18496)

Fixed various grammatical and structural issues in the article.
This commit is contained in:
Nitin Sharma
2018-10-14 21:49:07 +05:30
committed by Quincy Larson
parent bf743451dc
commit ce92f504d9

View File

@ -2,39 +2,39 @@
title: Php Arrays
---
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.
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 it's easier to define an array of length 100.
There are three different kind of arrays and each array value is accessed using an ID c which is called array index.
There are three different kind of arrays and each array value is accessed using an ID which is called array index.
Numeric array An array with a numeric index. Values are stored and accessed in linear fashion.
Indexed array An array with a numeric index. Values are stored and accessed in a linear fashion.
Associative array An array with strings as index. This stores element values in association with key values rather than in a strict linear index order.
Associative array An array with strings as the index. This stores element values in association with key values rather than in a strict linear index order.
Multidimensional array An array containing one or more arrays and values are accessed using multiple indices
Multidimensional array An array containing one or more arrays and values are accessed using multiple indices.
NOTE Built-in array functions is given in function reference PHP Array Functions
NOTE Built-in array functions are given in the function reference PHP Array Functions section.
### Numeric Array
### Indexed Arrays
These arrays can store numbers, strings and any object but their index will be represented by numbers. By default array index starts from zero.
#### Example
Following is the example showing how to create and access numeric arrays.
Following is the example showing how to create and access indexed arrays.
Here we have used array() function to create array. This function is explained in function reference.
Here we have used array() function to create an array. This function is explained in function reference.
```
<html>
<body>
<?php
/* First method to create array. */
$numbers = array( 1, 2, 3, 4, 5);
/* First method to create an array. */
$numbers = array(1, 2, 3, 4, 5);
foreach( $numbers as $value ) {
echo "Value is $value <br />";
}
/* Second method to create array. */
/* Second method to create an array. */
$numbers[0] = "one";
$numbers[1] = "two";
$numbers[2] = "three";
@ -66,33 +66,34 @@ Value is five
```
### Associative Arrays
The associative arrays are very similar to numeric arrays in term of functionality but they are different in terms of their index. Associative array will have their index as string so that you can establish a strong association between key and values.
The associative arrays are very similar to Indexed arrays in term of functionality but they are different in terms of their index. The elements in an associative array have their indices as strings so that you can establish a strong association between the keys and values.
To store the salaries of employees in an array, a numerically indexed array would not be the best choice. Instead, we could use the employees names as the keys in our associative array, and the value would be their respective salary.
To store the salaries of employees in an array, a numerically indexed array would not be the best choice. Instead, we can use the employee's names as the keys in our associative array, and the value will be their respective salaries.
NOTE Don't keep associative array inside double quote while printing otherwise it would not return any value.
NOTE Don't keep an associative array inside double quotes while printing otherwise it will not return any value.
#### Example
```
Example
<html>
<body>
<?php
/* First method to associate create array. */
/* First method to associate create an array. */
$salaries = array("mohammad" => 2000, "qadir" => 1000, "zara" => 500);
echo "Salary of mohammad is ". $salaries['mohammad'] . "<br />";
echo "Salary of qadir is ". $salaries['qadir']. "<br />";
echo "Salary of zara is ". $salaries['zara']. "<br />";
echo "Salary of Mohammad is ". $salaries['mohammad'] . "<br />";
echo "Salary of Qadir is ". $salaries['qadir']. "<br />";
echo "Salary of Zara is ". $salaries['zara']. "<br />";
/* Second method to create array. */
/* Second method to create an array. */
$salaries['mohammad'] = "high";
$salaries['qadir'] = "medium";
$salaries['zara'] = "low";
echo "Salary of mohammad is ". $salaries['mohammad'] . "<br />";
echo "Salary of qadir is ". $salaries['qadir']. "<br />";
echo "Salary of zara is ". $salaries['zara']. "<br />";
echo "Salary of Mohammad is ". $salaries['mohammad'] . "<br />";
echo "Salary of Qadir is ". $salaries['qadir']. "<br />";
echo "Salary of Zara is ". $salaries['zara']. "<br />";
?>
</body>
@ -102,21 +103,21 @@ Example
This will produce the following result
```
Salary of mohammad is 2000
Salary of qadir is 1000
Salary of zara is 500
Salary of mohammad is high
Salary of qadir is medium
Salary of zara is low
Salary of Mohammad is 2000
Salary of Qadir is 1000
Salary of Zara is 500
Salary of Mohammad is high
Salary of Qadir is medium
Salary of Zara is low
```
### Multidimensional Arrays
A multi-dimensional array each element in the main array can also be an array. And each element in the sub-array can be an array, and so on. Values in the multi-dimensional array are accessed using multiple index.
In a multi-dimensional array, each element in the main array can also be an array. And each element in the sub-array can be an array, and so on. Values in the multi-dimensional array are accessed using multiple indices.
Example
In this example we create a two dimensional array to store marks of three students in three subjects
#### Example
In this example, we will create a two dimensional array to store marks of three students in three subjects
This example is an associative array, you can create numeric array in the same fashion.
This example is an associative array, you can create an indexed array in a similiar fashion.
```
<html>
@ -144,13 +145,13 @@ This example is an associative array, you can create numeric array in the same f
);
/* Accessing multi-dimensional array values */
echo "Marks for mohammad in physics : " ;
echo "Marks for Mohammad in physics : " ;
echo $marks['mohammad']['physics'] . "<br />";
echo "Marks for qadir in maths : ";
echo "Marks for Qadir in maths : ";
echo $marks['qadir']['maths'] . "<br />";
echo "Marks for zara in chemistry : " ;
echo "Marks for Zara in chemistry : " ;
echo $marks['zara']['chemistry'] . "<br />";
?>
@ -161,7 +162,7 @@ This example is an associative array, you can create numeric array in the same f
This will produce the following result
```
Marks for mohammad in physics : 35
Marks for qadir in maths : 32
Marks for zara in chemistry : 39
Marks for Mohammad in physics : 35
Marks for Qadir in maths : 32
Marks for Zara in chemistry : 39
```