Array is a set of variables that have the same name but are distinguished by the index . useful for example in an array of programming that requires a few variables that will hold the data with the same data type and will get similar treatment . Now, rather than worry about a different variable , it is better to use a variable name but are distinguished by their respective indices.
Index may be a number or string . If the index -shaped array of numbers, then the array will be called indexed arrays (vector ) , whereas if the index on a string, then the array will be called associative arrays.
Rules of writing an array is as follows :
$ nama_array [ no_indeks ] or $ nama_array [" str_indeks "]Nama_array is the name used as an array variable .
No_indeks is the index number for the vector array. No index starts from zero .
Str_indeks is the string that is used as an index for the associative array .
To declare an array you can use an explicit declaration , that means directly assign values to an array, for example :
$ no_index [ 1 ] = 100To exclude the value of an array variable in the echo function is used in a way that is separate use braces . So if the normal variable is written as follows :
$ str_index [" str "] = 250
<? php echo "The value of the variable is $ var "; ? >So if you use an array of writing are as follows :
<? php echo "The value of the variable array is { $ no_index [ 1 ] } " ; ? >Use this new curly brackets known in PHP 4 , while in PHP 3 , to do something like the above can be used method for combining the string as follows :
" Value is an array variable . " $ no_index [ 3 ] ;Array which has an index called the one -dimensional array , while the index of an array with more than one is called multi -dimensional arrays . Vector arrays and associative arrays can be combined into multi -dimensional arrays , so an index that is used on each dimension should not equally shared numbers or strings .
In addition to the explicit declarations , arrays can also be declared with an array of functions ( ) . The syntax of the function array ( ) are as follows :
Array ( index = > value ) or Array (value )If you follow the second form , then the index will be added automatically by PHP . The following will be given an example of using an array function ( ) with some variations, for example :
<HTML>
<HEAD>
<TITLE> Array < / TITLE >
< / HEAD >
<BODY>
<? php< / BODY >
/ / without using the index
$ arrayA = array ( 1,2,3,4,5,6 ) ;
foreach ( $ arrayA as $ index = > $ value ) {
echo " \ $ arrayA [$ index ] = > $ value ";
echo " <br /> ";
}
echo " <br /> <br /> ";
/ / use the index but not serial
$ arrayB = array ( 1 = > 1.3 = > 2.5 = > 3.4 = > 4.0 = > 5.2 = > 6 ) ;
foreach ( $ arrayB as $ index = > $ value ) {
echo " \ $ arrayB [$ index ] = > $ value ";
echo " <br /> ";
}
echo " <br /> <br /> ";
/ / not all use the index ,
/ / index php will automatically give
$ arrayC = array ( 1 = > 1,2,3,4,0 = > 5.6 ) ;
foreach ( $ arrayC as $ index = > $ value ) {
echo " \ $ arrayC [$ index ] = > $ value ";
echo " <br /> ";
}
echo " <br /> <br /> ";
/ / two- dimensional array
/ / first dimension associative
/ / two -dimensional vector
$ duadim = array (
" prog " = > array ( 1 = > "HTML " , " PHP " , "JavaScript " ) ,
" numbers " = > array ( 1 = > 1,2,3 ) ,
" experience " = > array ( 1 = > " Beginer " , " Middletown " , " Advance " )
);
foreach ( $ duadim as $ k = > $ v ) {
$ x = 1 ;
foreach ( $ v as $ x = > $ y ) {
echo " \ $ duadim [$ k ] = > \ $ v [$ x ] = > $ y <br /> ";
$ x + + ;
}
}
?>
< / HTML >
Next we will discuss other functions that used to work with arrays.
Function count ( ) and sizeof ( )
Both these functions have the same utility , namely counting the number of elements owned by an array . The syntax is as follows :
count ( $ array)Example :
sizeof ( $ array)
$ A [ 0 ] = 1 ;Function array_push ( )
$ A [ 1 ] = 3 ;
$ A [ 2 ] = 5 ;
$ total = count ( $ a ) ;
/ / variable $ amount would be worth three
$ B [" fruit "] = " watermelon ";
$ B [" vegetable "] = " carrots ";
$ B [ "flesh "] = " chicken " ;
$ B [" key "] = "rice ";
$ total = sizeof ( $ b ) ;
/ / variable $ amount will be worth 4
This function is used to insert new elements into an array. This new element is inserted at the very end . The syntax is as follows :
Array_push ( $ array , value [ value ,...])Values are new values that will be inserted into the array. Example :
$ program = array ( "HTML " , " PHP " ) ;
array_push ( $ program , " CSS " , "JavaScript " ) ;
/ / members $ program is now the " HTML " , " PHP " , " CSS " , "JavaScript "
array_splice function ( )
This function is used to retrieve / remove one or more elements of the array and replace it with new. The syntax is as follows :
array_splice ( $ array , position [ , length [, replacement ] ] )
location parameter indicates how many elements from these arrays will be replaced / removed. If the value is positive , then the element is calculated from the front , if negative , then the element is computed from the rear .
length parameter - if the value is positive - show the number of elements that will be replaced / removed. If negative , then the number of elements left behind ( not replaced / removed ) was calculated from the back of a length parameter value . If the length parameter is not specified , then the elements of the array starting at the location indicated parameters to the last element will be replaced / removed.
Parameter is the replacement values that replace the elements that have been discarded.
Example :
<HTML>
<HEAD>
<TITLE> Array < / TITLE >
< / HEAD >
<BODY>
<? php< / BODY >
echo "Members of the initial array is: <br /> ";
$ input = array ( "HTML " , " PHP " , " CSS " , "JavaScript " ) ;
print_r ( $ input) ;
echo " <br /> <br /> ";
echo " array_splice ( \ $ input , 2 ) ";
echo " <br /> ";
$ input = array ( "HTML " , " PHP " , " CSS " , "JavaScript " ) ;
array_splice ( $ input , 2 ) ;
print_r ( $ input) ;
echo " <br /> <br /> ";
echo " array_splice ( \ $ input , 1 , -1 ) ";
echo " <br /> ";
$ input = array ( "HTML " , " PHP " , " CSS " , "JavaScript " ) ;
array_splice ( $ input , 1 , -1 ) ;
print_r ( $ input) ;
echo " <br /> <br /> ";
echo " array_splice ( \ $ input , 1 , count ( \ $ input) , \ " AJAX \ " ) ";
echo " <br /> ";
$ input = array ( "HTML " , " PHP " , " CSS " , "JavaScript " ) ;
array_splice ( $ input , 1 , count ( $ input) , " AJAX " ) ;
print_r ( $ input) ;
echo " <br /> <br /> ";
echo " array_splice ( \ $ input , -1.1 , array ( \ " JQuery \ " , \ " Framework \ "))";
echo " <BR> ";
$ input = array ( "HTML " , " PHP " , " CSS " , "JavaScript " ) ;
array_splice ( $ input , -1 , 1 , array ( " JQuery " , " Framework " ) ) ;
print_r ( $ input) ;
?>
< / HTML >
Happy learning : - D
0 komentar:
Post a Comment