PHP Array:
- Numeric array - An array with a numeric index. Values are stored and accessed in 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.
- Multidimensional array - An array containing one or more arrays and values are accessed using multiple indices
PHP Numeric Array:
Numeric arrays is used to store numbers, strings and any object but their index will be prepresented by numbers. By default array index starts from zero.
Here we have used array() function to create array. This function is explained in function reference.
A simple example of numeric array is describe below :Example : First method to create array
<?php$city = array("jaipur","aligarh","agra"); foreach( $city as $value ) { echo "Value is $value <br />"; } ?>Output
Value is jaipur
Value is aligarh
Value is agraSecond method to create array
<?php$name[0] = "rex"; $name[1] = "raj singh"; $name[2] = "www"; $name[3] = "maddy"; foreach( $name as $value ) { echo "Value is $value <br />"; } ?>Output
Value is rex
Value is raj singh
Value is www
Value is maddyPHP Associative Array:
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.
In associative array we declare index of array according to our business requirement or their individual value.Syntax
<?phparray( key1 => value, key2 => value2, key3 => value3, ... ) ?>A simple example of associative array declaration is given below :
<?php$city = array( "jaipur" => "jaipur is called pink city", "aligarh" => "aligarh is my local city", ); // as of PHP 5.4 $array = [ "jaipur" => "jaipur is called pink city", "aligarh" => "aligarh is my local city", ]; ?>Second method to create array
<?php$city = array( "rex" => "jaipur", "www" => "faizabad", "raj" => "aligarh" ); echo "city of rex-".$city['rex']."<br />"; echo "city of www-".$city['www']."<br />"; echo "city of raj-".$city['raj']."<br />"; /* Second method to create array. */ $city['max'] = "agra"; $salaries['adil'] = "delhi"; $salaries['zara'] = "ajmer"; echo "city of max-".$city['rex']."<br />"; echo "city of adil-".$city['www']."<br />"; echo "city of zara-".$city['raj']."<br />"; ?>Output
city of rex-jaipur
city of www-faizabad
city of raj-aligarh
city of max-jaipur
city of adil-faizabad
city of zara-aligarhPHP Multi Dimensional Array:
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.Example
In this example we create a two dimensional array to store id and password of three students :
This example is an associative array, you can create numeric array in the same fashion.<?php$data = array( "rex" => array ( "id" => "rex@gmail.com", "password" => "111" ), "www" => array ( "id" => "www@gmail.com", "password" => "333" ), "raj" => array ( "id" => "thakur.raj108@gmail.com", "password" => "111" ), ); /* Accessing array values */ echo $data['rex']['id']."<br />"; echo $data['rex']['password']."<br />"; echo $data['www']['id']."<br />"; echo $data['www']['password']."<br />"; echo $data['raj']['id']."<br />"; echo $data['raj']['password']."<br />"; ?>Output
rex@gmail.com
111
www@gmail.com
333
thakur.raj108@gmail.com
111
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.
In PHP you can give a single definition to a variable. If you wanted to add a list of items to a variable, you would be creating an array. Below is how you create an array in PHP.
In PHP you can give a single definition to a variable. If you wanted to add a list of items to a variable, you would be creating an array. Below is how you create an array in PHP.
Example
<?php?>Output
Array ( [0] => jaipur [1] => agra [2] => aligarh )
Note- In our example above, we can then print the contents of the array using print_r().
An array in PHP is actually an ordered map. A map is a type that associates values to keys. This type is optimized for several different uses; it can be treated as an array, list (vector), hash table (an implementation of a map), dictionary, collection, stack, queue, and probably more. As array values can be other arrays, trees and multidimensional arrays are also possible.
Types Of Array in php
There are three different kind of arrays in php and each array value is accessed using an ID which is called array index.
No comments:
Post a Comment