Basic Tutorial On Arrays

free web hosting
Free Web Hosting, No Ads > CONTRIBUTE > Computers > Programming Languages > PHP 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

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.

 

 

 


Reply

electron
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

 

 

 


Reply

farsiscript
hi dear electron i agree your code
good job

Reply

masterio
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

Reply



Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

(Maximum characters: 10,000)
You have characters left.
Confirm Code:

Recent Queries:-
  1. "visual basic 6" "array_push" - 170.91 hr back. (1)
Similar Topics

Keywords : basic, arrays

  1. Arrays Outside A Function
    Need to have arrays available to all functions. (3)
  2. Problem With Global Arrays
    (2)
    I've got a problem with global variables. /sad.gif" style="vertical-align:middle" emoid=":("
    border="0" alt="sad.gif" /> I've read and re-read the stuff about that on php.net and did that
    a few times more, and I'm still not able to make it work. Could somebody please fix my code?
    This is the primary page: include "getLvlFromHigh.func"; $user = $_GET ; $skill =
    $_GET ; $goal = $_GET ; $userXp = getLvlFromHigh($user); $xpTable =
    array( 31 => 14833, 32 => 16456, 33 => 18247, 34 => 20224, 35 => 22406, 36 =>....
  3. Converting Characters In A Variable To Individual Values In An Array
    turning variables into arrays (2)
    Say I have a variable such as $nav_item and it had to contents Home . IE: CODE
    $nav_item = 'Home'; How would I make so that $nav_item was an array and
    had the following contents? CODE $nav_item = array ('h', 'o',
    'm', 'e'); With the case changing (ie H would become h and U
    would become u ) EDIT: Okay found out that I could change the case with
    array_change_key_case ($nav_item, CASE_LOWER); ....
  4. Alternative To File()?
    file stored in arrays (4)
    Hey guys. I was wondering if there is an alternative to using file(). I'm trying to read a file
    and store it into an array without using the file function. Before, I was doing something like this:
    CODE <?php  $file = file("http://yahoo.com"); ?> Simple.
    It gets the contents of index.html at yahoo.com and store each line in an array. Well, I was
    wondering if there is another code that could do that without using the file function. Thanks for
    helping!....
  5. Appending Arrays
    (4)
    When you have a normal variable say $string and you want to add more to it you would use the .=
    construct, but how does one go about doing that with an array, I have an IF structure that determins
    what options the user has selected, so I have something like: CODE $array = NULL; IF
    (isset($_POST['op1])){ $array .= array(1, 2, 3, 4); }
    IF (isset($_POST['op2'])){ $array .= array(5, 6, 7,
    8); } So if there is a value for op1 then the array contains 1,2,3,4 is th....

    1. Looking for basic, arrays

Searching Video's for basic, arrays
advertisement



Basic Tutorial On Arrays



 

 

 

 

ADD REPLY / Got an Opinion! a humble request :-) RAPID SEARCH! Free Hosting [X]
Express your Opinions, Thoughts or Contribute more info. to help others.
Ask your Doubts & Queries to get answers, So that "Together We can help others!"
Register FREE for AD-FREE forum, Create your own topics, Ask Questions, track topics, setup subscriptions & notifications and Get a Free Website w/ Email and FTP.
500MB Space *No Ads*, CPanel, FTP, PHP, MySQL, EMails - 100% FREE