Welcome Guest ( Log In | Register)



 
Reply to this topicStart new topic
> Basic Tutorial On Arrays
imacul8
post Aug 20 2006, 02:14 AM
Post #1


Member [Level 3]
******

Group: Members
Posts: 95
Joined: 21-May 06
From: Adelaide, Australia
Member No.: 24,017



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

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


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.
QUOTE


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
QUOTE
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.


This post has been edited by BuffaloHELP: Aug 20 2006, 03:28 PM
Go to the top of the page
 
+Quote Post
electron
post Aug 21 2006, 03:00 AM
Post #2


Premium Member
********

Group: Members
Posts: 162
Joined: 10-May 06
Member No.: 23,375



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

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

print_r($var);


This will print:

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


Also you can declare an array within an array:

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

print_r($var);



This will print:

CODE

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



To print an array better use the following:

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


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

Hope this helps
Go to the top of the page
 
+Quote Post
farsiscript
post Aug 21 2006, 09:09 AM
Post #3


Super Member
*********

Group: Members
Posts: 357
Joined: 8-April 06
Member No.: 21,487



hi dear electron i agree your code
good job
Go to the top of the page
 
+Quote Post
masterio
post Aug 25 2006, 10:17 AM
Post #4


Member [Level 1]
****

Group: Members
Posts: 50
Joined: 25-August 06
Member No.: 28,897



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

CODE

// 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.

ph34r.gif

This post has been edited by masterio: Aug 25 2006, 10:18 AM
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. Converting Characters In A Variable To Individual Values In An Array(2)
  2. Problem With Global Arrays(2)
  3. Arrays Outside A Function(3)


 



- Lo-Fi Version Time is now: 8th October 2008 - 04:42 AM