• Using glob() To Find Files and array_map() to allocate function for each value of array

    Using glob() To Find Files and array_map() to allocate function for each value of array

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    
    <!--?php  $files1 = glob('*.php');  //File With PHP Extention in current Folder Where This Script is located . Result is files with .php extention /* output looks like: Array (     [0] =--> phptest.php
        [1] =&gt; pi.php
    )
    */
     
    $files2 = glob('D:/xampp/htdocs/mYscHoOlsQuarD.com/user/*.php');  //with specific path you get detail For that extention's File . Result is all files with php extention . Here i can use specific path as 'D:/xampp/htdocs/mYscHoOlsQuarD.com/user/'
     
    // get all php files AND txt files
     
    $files3 = glob('*.{php,txt}', GLOB_BRACE);
     
    /* output looks like:
    Array
    (
        [0] =&gt; phptest.php
        [1] =&gt; pi.php
        [4] =&gt; log.txt
        [5] =&gt; test.txt
    )
    */
     
    $files4 = glob('a*.php'); // List of .php extention Files start with 'a' . You may chnages 'a' replace with any one of the a to z .
     
    //If you want to get the full path to each file, you can just call the realpath() function on the returned values:
     
        $files5 = glob('../images/a*.jpg');
     
        // applies the function to each array element
        $files5 = array_map('realpath',$files5);
        print_r($files5);
     
        /* output looks like:
        Array
        (
            [0] =&gt; C:\wamp\www\images\apple.jpg
            [1] =&gt; C:\wamp\www\images\art.jpg
        )
        */
     
    /*
    The array_map() function sends each value of an array to a user-made function, and returns an array with new values, given by the user-made function.
    */
     
    function myfunction($v1,$v2)
    {
    if ($v1===$v2)
      {
      return "same";
      }
    return "different";
    }
    $a1=array("Horse","Dog","Cat");
    $a2=array("Cow","Dog","Rat");
    print_r(array_map("myfunction",$a1,$a2));
     
    /*output
    Array ( [0] =&gt; different [1] =&gt; same [2] =&gt; different )
    */
     
    ?&gt;
    Tags:

Comments on this post

Leave a Reply

  • Security Code :


    8 − one =