HOME       >>       Programming

Basic Tutorial On Arrays


imacul8

In php u can declare arrays by two methods

 

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

 

$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 :

 

print "arrayname[0]"; => prints basicprint "arrayname[1]"; => prints arrayprint "arrayname[2]"; => prints tutorial

To find out the number of elements in an array u can use the count() function.

 

$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

 

$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

 

$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

 

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

 

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.

Source http://forums.xisto.com/no_longer_exists/

Hope this helps anyone in need of some guidance using PHP arrays. For more help u can contact me via PM on here.

 

Notice from BuffaloHELP:
Try reading the page you're copying and pasting. It clearly says

You MAY NOT redistribute this article (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.


electron

arrays can also be given keys using the array function. This is very useful and is better to understand . e.g.

$var = array('one' => 1, 'two' =>2, 'three'=>3);print_r($var);

This will print:

Array([one] => 1,[two] =>2,[three]=>3)

Also you can declare an array within an array:

$var = array('one' => array(1, 2, 3), 'two' =>2, 'three'=>3);print_r($var);


This will print:

Array([one] => Array( [0]=>1, [1]=>2, [2]=>3 ),[two] =>2,[three]=>3)


To print an array better use the following:

echo '<pre>';print_r($var);echo '</pre>';

There are more functions to do much with arrays.
Read the PHP Manual.

Hope this helps

farsiscript

hi dear electron i agree your code good job


masterio

This method is for printing the array values using loop method:

// declare the array$myarr = array(5, 4, 3, 2, 1);// use while loop and use end() to make the array start from behind$val = end($myarr);while ($val) { print $val.'<br />'; $val = prev($myarr);}

It will print:
1
2
3
4
5

we can use prev(), next(), current(), end() to navigate through array element.




VIEW ORIGINAL REGISTER GET FREE HOSTING

Xisto.com offers Free Web Hosting to its Members for their participation in this Community. We moderate all content posted here but we cannot warrant full correctness of all content. While using this site, you agree to have read and accepted our terms of use, cookie and privacy policy. Copyright 2001-2019 by Xisto Corporation. All Rights Reserved.