Sunday 9 June 2013

INTRODUCING ARRAYS AND BASIC ARRAY FUNCTIONS IN PHP

In this post I will be explaining basics of arrays in php, and some of the inbuilt array functions.
Arrays are the contiguous and homogeneous collection of data, that can be referred through a common name. Arrays come under the static data types. Arrays of any type can be created and can have one or more dimensions. Any element of array can be accessed through a common array name and using the index number.


Source code # 1 for building a simple array:-
/***********************************************************************************************************/
<html>
                <head>
                                <title>arrays</title>
                </head>
               
                <body>
                                <?php
                                                $arr=array(1,2,3,4,5,6);
                                                echo "second element = ".$arr[1]."<br />";
                                ?>
                               
                                <pre>
                                                <?php print_r($arr);?>
                                </pre>
                </body>
</html>
/********************************************************************************/
Output:-
second element = 2
   Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
    [5] => 6
)
 

The pre tags are used for the pre-formatting of the text that prints the array in more readable format. 



Source code # 2
/* implementing heterogeneous arrays */
/*******************************************************************************/

<html>
                <head>
                                <title>Heterogeneous Arrays</title>
                </head>
               
                <body>
                                <?php
                                                $arr=array(6,"fox",8,"deer","lion");
                                ?>
                                <pre>
                                                <?php
                                                                print_r($arr);
                                                ?>
                                </pre>
                </body>

</html>
/******************************************************************************/
Output:-
Array
(
    [0] => 6
    [1] => fox
    [2] => 8
    [3] => deer
    [4] => lion
)
 


Source code # 3 for implementing basic array functions:-
/******************************************************************************/
<html>
                <head>
                                <title>Array Functions</title>
                </head>
               
                <body>
                                <?php
                                                $arr=array(1,2,3,4,5,6,7,8,9,15,14,13,12,11,10);
               
                                                /*to find the total number of elements in array */
                                                echo "Total number of elements = ".count($arr)."<br />";
                                               
                                                /* to find the maximum element of array*/
                                                echo "maximum number in array is = ".max($arr)."<br />";
                                               
                                                /* to find the minimum element of array*/
                                                echo "minimum number in array is = ".min($arr)."<br />";
                                               
                                                /* to sort the elemets of the array...*/
                                                echo "Array in ascending order is = "."<br />";
                                                sort($arr);
                                ?>
                                <pre>
                                                <?php
                                                                print_r($arr);
                                                ?>
                                </pre>
                                <?php
                                                echo"<br />";
                                               
                                                /* to sort the array in descending order */
                                                rsort($arr);
                                ?>
                                <pre>
                                                <?php
                                                                echo"Array in descending order is : "."<br />";
                                                                print_r($arr);
                                                ?>
                                </pre>
                                <?php
                                                echo"<br />";
                                ?>
                               
                                <pre>
                                <br />
                                <?php
                                                $str=implode(",",$arr);

                                                /* using implode function */
                                                echo "after using implode function : string = ".$str;

                                                /* usin explode function */
                                                echo"<br /><br />the array obtained after using explode function : <br />";

                                                print_r(explode(",",$str));
                                ?>
                                <br />
                                </pre>
                               
                                <?php
                                                /*checking whether a number exists in array or not ...
                                                 *if number dont exists in array then it dont prints anything.
                                                 *but if number exists in array then it prints 1
                                                 */
                                                echo "12 is in the array ? ".in_array(12,$arr)."<br />";
                                                echo "122 is in the array ? ".in_array(122,$arr)."<br />";
                                ?>
                               
                </body>
</html>
/********************************************************************************/
Output:-
Total number of elements = 15
maximum number in array is = 15
minimum number in array is = 1
Array in ascending order is = 
   Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
    [5] => 6
    [6] => 7
    [7] => 8
    [8] => 9
    [9] => 10
    [10] => 11
    [11] => 12
    [12] => 13
    [13] => 14
    [14] => 15
)
  

   Array in descending order is : 
Array
(
    [0] => 15
    [1] => 14
    [2] => 13
    [3] => 12
    [4] => 11
    [5] => 10
    [6] => 9
    [7] => 8
    [8] => 7
    [9] => 6
    [10] => 5
    [11] => 4
    [12] => 3
    [13] => 2
    [14] => 1
)
  

  

  after using implode function : string = 15,14,13,12,11,10,9,8,7,6,5,4,3,2,1

the array obtained after using explode function : 
Array
(
    [0] => 15
    [1] => 14
    [2] => 13
    [3] => 12
    [4] => 11
    [5] => 10
    [6] => 9
    [7] => 8
    [8] => 7
    [9] => 6
    [10] => 5
    [11] => 4
    [12] => 3
    [13] => 2
    [14] => 1
)
  

  
12 is in the array ? 1
122 is in the array ? 

No comments:

Post a Comment