1. Using the Array function
2. Using the Array Identifier
Array Function
To define an array consisting of three elements using the array function we write the following code
CODE
$arrayname= array("basic","array","tutorial");
This declares an array with the name $arrayname containing 3 elements.
To access these elements u have to use the array name along with the index in square brackets.
** Note: php arrays start from 0 index
To print the elements of the above array u can use the code :
CODE
print "arrayname[0]"; => prints basic
print "arrayname[1]"; => prints array
print "arrayname[2]"; => prints tutorial
print "arrayname[1]"; => prints array
print "arrayname[2]"; => prints tutorial
To find out the number of elements in an array u can use the count() function.
CODE
$noofelements = count($arrayname); print "$noofelements";
This will print 3 since there is 3 elements in the above array.
Array Identifier
Now the second method to define arrays in php is using array identifiers
CODE
$arrayname[0]="basic"; $arrayname[1]="array"; $arrayname[2]="tutorial";
This will declare an array named $arrayname with 3 elements. U can also assign a value to a array like this
CODE
$arrayname[]="hello";
This will cause the string hello to be stored in the next available position in the array $arrayname
.ie position 4
Now if u print this
CODE
print "$arrayname[3]" this will print hello
There are also quite a few built in functions that can be used with arrays. Here i will list them and show how they work
QUOTE
1. array_merge
This function is used to combine two arrays to produce a third array which is a combination of those two arrays passed as arguments to array_merge function
$array1= array(1,2,3);
$array2= array(4,5,6);
$array3=array_merge($array1,$array2);
now $array3 will contain (1,2,3,4,5,6)
2. array_shift
This function returns the first element from the array passed as the argument to this function.
$array1=array(1,2,3);
$firstelement=array_shift($array1);
now $firstelement will contain 1
3. array_push
This function is used to add elements to an array and this function returns the no of elements in the resultant array.
$array1=array(1,2,3);
array_push($array1,4,5,6);
now $array1 will contain 1,2,3,4,5,6 elements
** Note that the main difference between array_merge and array_push is that in array_push the array passed itself is modified unlike in array_merge in which a new array is created after merging leaving the original array as it is.
3. array_slice
This function is used to retrieve a part of an array.
$array1=array(1,2,3);
$array2=array_slice($array1,1,2);
now $array2 will contain 2,3 as its elements.
** Note that the second argument to this function is the staring index of the original array from where the retreiving of elements should start and the third is the no of elements of the original array which should be retreived. If this is absent then all elements from the starting position will be retreived and also if the second argument is negative then the start index for retreving will be from end of the original array. If the third argument is negative then the function will retreive that many elements starting from the start position specified as second argument from the end of the original array.
php has got many sorting function
1. sort(arrayname)
This function will sort the elements of array passed as argument in numerical or alphabetical order depending on the elements.
2. asort(arrayname)
This function will sort the elements of an associative array passed as argument in numerical or alphabetical order of the values of the array .
3. ksort(arrayname)
This function will sort the elements of an associative array passed as argument in numerical or alphabetical order of the keys of the array.
4.arsort(arrayname)
Same as asort but sorting is in reverse order.
5. krsort(arrayname)
Same as ksort but sorting is in reverse order.
6. rsort(arrayname)
Same as sort but sorting is in reverse order.
This function is used to combine two arrays to produce a third array which is a combination of those two arrays passed as arguments to array_merge function
$array1= array(1,2,3);
$array2= array(4,5,6);
$array3=array_merge($array1,$array2);
now $array3 will contain (1,2,3,4,5,6)
2. array_shift
This function returns the first element from the array passed as the argument to this function.
$array1=array(1,2,3);
$firstelement=array_shift($array1);
now $firstelement will contain 1
3. array_push
This function is used to add elements to an array and this function returns the no of elements in the resultant array.
$array1=array(1,2,3);
array_push($array1,4,5,6);
now $array1 will contain 1,2,3,4,5,6 elements
** Note that the main difference between array_merge and array_push is that in array_push the array passed itself is modified unlike in array_merge in which a new array is created after merging leaving the original array as it is.
3. array_slice
This function is used to retrieve a part of an array.
$array1=array(1,2,3);
$array2=array_slice($array1,1,2);
now $array2 will contain 2,3 as its elements.
** Note that the second argument to this function is the staring index of the original array from where the retreiving of elements should start and the third is the no of elements of the original array which should be retreived. If this is absent then all elements from the starting position will be retreived and also if the second argument is negative then the start index for retreving will be from end of the original array. If the third argument is negative then the function will retreive that many elements starting from the start position specified as second argument from the end of the original array.
php has got many sorting function
1. sort(arrayname)
This function will sort the elements of array passed as argument in numerical or alphabetical order depending on the elements.
2. asort(arrayname)
This function will sort the elements of an associative array passed as argument in numerical or alphabetical order of the values of the array .
3. ksort(arrayname)
This function will sort the elements of an associative array passed as argument in numerical or alphabetical order of the keys of the array.
4.arsort(arrayname)
Same as asort but sorting is in reverse order.
5. krsort(arrayname)
Same as ksort but sorting is in reverse order.
6. rsort(arrayname)
Same as sort but sorting is in reverse order.
QUOTE
Hope this helps anyone in need of some guidance using PHP arrays. For more help u can contact me via PM on here.

