returning an array from a function

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Chris

    returning an array from a function

    I am clearly missing something simple here....

    I have a function (fileTree) that recursively reads all the files in a
    directory passed to it, and returns an array of the files, I have included
    the code for this function below.

    When I call this function, the bit of debug code at the end will correctly
    print the array of files I am expecting, and then returns.

    However, what is returned form the function isnt the array, is a scalar that
    just contains the value of the last element in the
    array

    I am calling the function like this :

    $BrokenArray = array();
    $BrokenArray = $this->fileTree($th is->ProjectRoot,". c");

    foreach (($this->fileTree($th is->ProjectRoot,". c")) as $wibble)
    {
    print "Broken Array : $wibble <br>\n";
    }


    So it seems that the array in the function contains the right data, and by
    doing "$BrokenArr ay = array();" i suspect I am making sure the varible it is
    being assinged to isnt a scalar, so the only thing I can think of of is that
    I need to do something fancy with the "return" statement so it knows it's a
    array, but so far I have not not found anything that would help...

    Cheers

    Chris





    function fileTree($direc tory,$ext)
    {

    $dir_array = array();
    $file_array = array();

    // what is the length of the extension
    $ExtLen = strlen($ext);

    if ($handle = opendir($direct ory))
    {
    while (false !== ($file = readdir($handle )))
    {
    if ($file != "." && $file != "..")
    {
    // this is a directory, so recurse down it
    if (is_dir($direct ory. "/" . $file))
    {
    $dir_array = array_merge($di r_array, $this->fileTree($dire ctory.
    "/" . $file,$ext));
    $file = $directory . "/" . $file;
    $dir_array[] = preg_replace("/\/\//si", "/", $file);
    }

    // this is a file, test it, and add it to list if need be
    else
    {

    $file = $directory . "/" . $file;

    // overall length of the $file string
    $FileLen = strlen ($file);

    // now match the last N chars with the extension
    if ((substr ($file,($FileLe n-$ExtLen),($File Len))) == $ext)
    {
    $this->fileArray = array_merge($th is->fileArray,$fil e);
    $file_array = array_merge($fi le_array,$file) ;
    }

    }
    }
    }

    closedir($handl e);
    }

    // print out the array of files, to prove it is getting all the files
    foreach ($file_array as $wibble)
    {
    print "Wibble : $wibble <br>\n";
    }

    return ($file_array);
    }


  • tim

    #2
    Re: returning an array from a function


    Chris wrote:

    Hi
    [color=blue]
    >
    > I have a function (fileTree) that recursively reads all the files in a
    > directory passed to it, and returns an array of the files, I have included
    > the code for this function below.[/color]
    [color=blue]
    > if (is_dir($direct ory. "/" . $file))
    > {
    > $dir_array = array_merge($di r_array, $this->fileTree($dire ctory.
    > "/" . $file,$ext));
    > $file = $directory . "/" . $file;
    > $dir_array[] = preg_replace("/\/\//si", "/", $file);
    > }[/color]

    In the above code, the files found in the subdirectories aren't being
    added to $file_array.
    [color=blue]
    >
    > When I call this function, the bit of debug code at the end will correctly
    > print the array of files I am expecting, and then returns.[/color]

    Try print "In new dir<br>" and just before the foreach loop to see what
    I mean, its finding each file but its not passing what it found to the
    calling function.
    [color=blue]
    > foreach ($file_array as $wibble)
    > {
    > print "Wibble : $wibble <br>\n";
    > }
    >
    > return ($file_array);
    > }[/color]

    Hope that helps,

    Tim

    Comment

    • Tim Roberts

      #3
      Re: returning an array from a function

      "Chris" <Chris@dustbubb le.nospam.com> wrote:[color=blue]
      >
      >I am clearly missing something simple here....
      >...
      >I am calling the function like this :
      >
      > $BrokenArray = array();
      > $BrokenArray = $this->fileTree($th is->ProjectRoot,". c");[/color]

      Do you understand that the first statement is useless? This creates an
      empty array, assigns it to $BrokenArray, and then promptly deletes it, to
      be replaced by whatever fileTree returns.
      [color=blue]
      > foreach (($this->fileTree($th is->ProjectRoot,". c")) as $wibble)
      > {
      > print "Broken Array : $wibble <br>\n";
      > }
      >
      >
      >So it seems that the array in the function contains the right data, and by
      >doing "$BrokenArr ay = array();" i suspect I am making sure the varible it is
      >being assinged to isnt a scalar,[/color]

      No. You can't change what fileTree returns. If it returns an array,
      $BrokenArray will be an array. If it returns a scalar, $BrokenArray will
      be a scalar. $this->fileTree has absolutely no clue that $BrokenArray
      exists. If fileTree is supposed to return an array, then it must construct
      an array and return it.

      The other Tim gave you the rest of the hints you need to make this work.
      --
      - Tim Roberts, timr@probo.com
      Providenza & Boekelheide, Inc.

      Comment

      • Iván Sánchez Ortega

        #4
        Re: returning an array from a function

        -----BEGIN PGP SIGNED MESSAGE-----
        Hash: SHA1

        Chris wrote:
        [color=blue]
        > I am calling the function like this :
        >
        > $BrokenArray = array();
        > $BrokenArray = $this->fileTree($th is->ProjectRoot,". c");
        >
        > foreach (($this->fileTree($th is->ProjectRoot,". c")) as $wibble)
        > {
        > print "Broken Array : $wibble <br>\n";
        > }[/color]

        Debug that by using var_dump or print_r :

        $BrokenArray = array();
        $BrokenArray = $this->fileTree($th is->ProjectRoot,". c");
        var_dump($Broke nArray);


        By the way, remember that PHP is a dinammically-typed language: $BrokenArray
        is an array at first, but if you assign it a value of other type (an
        integer, for example), it becomes that type.

        - --
        - ----------------------------------
        Iván Sánchez Ortega -i-punto-sanchez--arroba-mirame-punto-net

        There may still be two superpowers on the planet: the United States and
        world public opinion.
        -- Patrick Tyler
        -----BEGIN PGP SIGNATURE-----
        Version: GnuPG v1.4.3 (GNU/Linux)

        iD8DBQFEdqDC3jc Q2mg3Pc8RAqiuAJ 9lALLpM6imxVP5J 9Zvn6nibiK3BgCc DlyI
        QilzLASAx7ZZrIo e7S5mkB4=
        =bi1H
        -----END PGP SIGNATURE-----

        Comment

        Working...