PHP Arrays

Posted on Updated on

PHP Arrays :

Array is very powerful concept which is use to store multiple values in one variable. Like we class roll no if we want to store 20 students roll no we will create 20 variables but with the help of array we can store 20 roll no in one variable. That’s mean it reduce the code which we write.

Difference between variable and array:

Rollno 1 = 1040

Rollno 2 = 1041

Rollno 3 = 1042

Rollno 4 = 1043

Rollno 5 = 1044

Rollno 6 = 1045

Rollno 7 = 1046

Rollno 8 = 1047

Rollno 9 = 1048

Rollno 10 = 1049

But in array we use only one variable $ rollno = array ( 1040, 1041, 1042, 1043, 1044, 1045, 1046, 1047, 1048, 1049 ) ;

We have three type of array in php language:

Indexed ( single array )

Multiple array

Associative array

Normal array syntax:

$ abc = array ( ‘ a ’,’ b ’,’ c ’ ) ;

Index array:- in this we fill the index value its start from 0 and end with total length -1.there are two ways first declare it with the numeric value which is start from 0 and second way to write it with normal way it automatic set the values.

Syntax:

$ array _ name = array ( “ value1 “ , “ value2 “ , “ value3 “ , “ value4 “ , “ value5 “ ) ;

Now manually :

$ array _ name [ 0 ] = value 1 ;

$ array _ name [ 1 ] = value 2 ;

$ array _ name [ 2 ] = value 3 ;

$ array _ name [ 3] = value 4 ;

$ array _ name [ 4 ] = value 5 ;

$ array _ name [ 5 ] = value 6 ;

Now lets move on how to access the array:

< ?php

$ abc = array ( ‘ a ’,’ b ’,’ c ’ ) ;

Echo $ abc [ 0 ] ;

? >

It show the value a as output.

Length of array:

Now the next question is how to find the length of an array?

Php provide count ( ) to find the length of an array . it count the element in the array and show the output in numeric form.

Syntax:

Count ( $ array _ name ) ;

Example:

< html >

< head >

< title > My array < / title >

< / head >

< body >

< h1 > Array Count function < / h1 >

< ? php

$ array _ name = array ( “ value 1 “ , “ value 2 “ , “ value 3 “ , “ value 4 “ , “ value 5 “ ) ;

$ count _ value = count ( $ array _ name ) ;

For ( $ i = 0 ; $ i < $ count _ value ; $ i++ )

{

Echo $ array _ name[ $ i]. “ < br / > “ ;

}

? >

< / body >

< / html >

It will show the all array values with next line we can show all values of the array with the help of for loop.

For more details and queries please feel free to email, visit or call us. Wishing you the very best for all your future endeavors.

Helpline: 9814666333, 8699444666

Email:info@technocampus.co.in

**************************************************************************************************************

Please fill the FORM and we shall contact you soon.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s