Function is_array ( )
Function is_array ( ) is used to check whether a variable is an array or not . If a variable is an array , this function will result in true value and if not the array will yield a value of false . The syntax is as follows :
Is_array (variable )Example :
<HTML>
<HEAD>
<TITLE> Array Part 2 < / TITLE >
< / HEAD >
<BODY>
<? php< / BODY >
$ var = array ( 1,2,3,4,5,6,7 ) ;
$ scan = is_array ( $ var ) ;
if ( $ scan == false) {
$ status = " no " ;
} else {
$ status = " " ;
}
echo " \ $ var = array ( 1,2,3,4,5,6,7 ); ";
echo " <br /> ";
echo " Variable \ $ var $ status is an array ";
?>
< / HTML >
In this example because the variable $ var is declared as an array , then the result given is the text " variable $ var is an array " . If if the variable $ var is declared as $ var = "array ", then the result is " variable $ var is not an array " , it 's all just be text "array " .
Function list ( )
Function list ( ) is used to retrieve an array of components as separate variables . The syntax is as follows :
List ( $ item1 , $ item2 , ... , $ itemn ) = arrayTotal variable $ item must be equal to or less than the number of components owned by the array .
Example :
<HTML>
<HEAD>
<TITLE> Array Part 2 < / TITLE >
< / HEAD >
<BODY>
<? php< / BODY >
$ program = array (' Bobo ',' Doraemon ',' Spiderman ') ;
list ( $ Magazine , $ comic , $ movie ) = $ program ;
echo " Type Books & Entertainment : ";
echo " <br /> ";
echo " Short Stories : $ Magazine ";
echo " <br /> ";
echo " Picture Stories : $ Comics ";
echo " <br /> ";
echo " Movies : $ Film ";
?>
< / HTML >
In this example $ program is the array with three components, namely " Bobo " , " Doraemon "and " Spiderman " . If the three components to be included in a separate variable , then the function list must provide three variables to hold these three components . Function list ( ) not have to "catch " the third component, but can be less than that. Example :
<HTML>
<HEAD>
<TITLE> Array Part 2 < / TITLE >
< / HEAD >
<BODY>
<? php< / BODY >
$ program = array (' Bobo ',' Doraemon ',' Spiderman ') ;
list ( $ Magazine , , $ movie ) = $ program ;
echo " Type Books & Entertainment : ";
echo " <br /> ";
echo " Short Stories : $ Magazine ";
echo " <br /> ";
echo " Movies : $ Film ";
?>
< / HTML >
The second example is only capturing the first and third components . Note that there must be an empty space and two commas between the variables that capture the first and third components . Empty space is a place that should be filled by the variables that would capture the second component .
Function split ( )
split function is used to split a string into an array based on a specific separator character . The syntax is as follows :
split ( character , text , [ limit ] )Character is the character used to separate arrays.
Text is the string that will be split into an array .
Limit the number of components to be produced.
Example :
<HTML>
<HEAD>
<TITLE> Array Part 2 < / TITLE >
< / HEAD >
<BODY>
<? php< / BODY >
$ date = " 27-02-1982 ";
list ( $ day , $ month , $ year ) = split ("-", $ date) ;
echo " Day = $ day ";
echo " <br /> ";
echo " Month = $ month ";
echo " <br /> ";
echo " Year = $ year ";
?>
< / HTML >
In this example the variable $ date contains the string " 27-02-1982 " . With the split function , the variable $ date is split into an array with the separator character " - ", so that becomes an array with component " 27 " , " 02 " , " 1982 " . array components are then collected into three pieces with the function of the variable list ( ) .
The function list can also use regular ekspression . For example , because people can write on a variety of ways , for example 02/27/1982 , 27-02-1982 , 02.27.1982 , then the separator character in the split function should be able to anticipate these differences , so its use is as follows :
list ( $ month , $ day , $ year ) = split ("[/.-]", $ date) ;Function join ( )
Function split ( ) is identical with the function explode ( ) .
This function is the inverse function basically split ( ) , which is used to assemble the components into a string array . The syntax is as follows :
join ( character array)Character is the character used for " glue " the components of the array.
Example :
<HTML>
<HEAD>
<TITLE> Array Part 2 < / TITLE >
< / HEAD >
<BODY>
<? php< / BODY >
$ var = array ( '03 ', '07 ', '1973 ') ;
$ date = join ("/", $ var ) ;
echo "$ date " ;
?>
< / HTML >
Function join ( ) function is identical to implode ( ) .
Function in_array ( )
Function in_array ( ) is used to check whether there is a particular value as a component in an array . The syntax is as follows :
in_array ( search , array , [ type ] )Search is the value that will be sought on whether there is an array .
The type is a boolean indicating whether the type of data will be included in the search. If a value is found but not the same data types , then the function in_array ( ) is considered a failure.
This function is case-sensitive .
Example :
<HTML>
<HEAD>
<TITLE> Array Part 2 < / TITLE >
< / HEAD >
<BODY>
<? php< / BODY >
$ program = array ( "HTML " , " PHP " , " CSS " , "JavaScript " ) ;
print_r ( $ program ) ;
$ search = "HTML ";
if ( in_array ( $ search , $ courses ) ) {
echo " $ search Web Base Program is in the array ";
} else {
echo " Database Program Web $ search is not in array ";
}
?>
< / HTML >
If the type is included examples is as follows :
<HTML>
<HEAD>
<TITLE> Array Part 2 < / TITLE >
< / HEAD >
<BODY>
<?< / BODY >
$ type = array ( '1 .10 ', 5.0 , 1.13) ;
if ( in_array ( '5 .0 ', $ type , TRUE ) ) {
echo " String \ " 5.0 \ " is in array ";
} else {
echo " String \ " 5.0 \ " not in array ";
}
echo " <br /> ";
if ( in_array ( 1:13 , $ type , TRUE ) ) {
echo " Numbers 1:13 is in the array ";
} else {
echo " Numbers 1:13 is not in array ";
}
?>
< / HTML >
In this second example , found a number of 1:13 , while the string " 5.0 " not found due on an array of numbers , not arrays. Good creative .
0 komentar:
Post a Comment