简体   繁体   中英

Passing an array to another function

Hey guys having some problems with an experiment of mine basically I am trying to read a file then put that into an array and would like to use that array in other functions.

   $totalBand = 0;
   $weekly = fopen('filepath', 'r');  //opening my file

       handler($weekly); //Calling my function

       function handler ($weekly) {
              $dataFile = array();

          while (!feof($weekly)) {
              $line=fgets($weekly);
              //add to array
              $dataFile[]=$line; //pitting file into an array
          }
          fclose($weekly);  //closing file
          return $dataFile; //returning the array
    }

    function band ($datafile) { 
    //function for counting data from each line of the array from 1st function
        $totalBand = 0;
        foreach ($datafile as $lines) {
            $pieces = explode(" ", $lines); //exploding file
            if ($totalBand > 0) { 
                $totalBand = $totalBand + $pieces [7]; 
                //extracting information from the 7th position in every line
            }
        }
        return $totalBand; // total value from the file
    }

    echo '<p>Total band = ' . $totalBand . 'bytes</p>';

I don't get any errors but I also don't get a result and I know the info is in the right position and in the file I think it's my first function that isn't getting the job done namely the returning/ passing of the array..

Any help would be great!

You're not storing your result from the function handler . Or calling the function band . Need something along the lines of

$result = handler($weekly);
$totalBand = band($result);
echo '<p>Total band = ' . $totalBand . 'bytes</p>';

Could chain or do this as a one liner if wanted, but it'd look pretty ugly.

Might want to readup on Scope , as it looks like you're trying to access local function variables from the global scope. After the function has completed, you can't access the variables inside of the function anymore, unless they're declared globally.

<?php
$totalBand = 0;
$weekly = fopen('filepath', 'r');  //opening my file

//Add this
$datafile=handler($weekly); //Calling my function

//Add This
$totalBand=band($datafile); //Calculating datafile

function handler ($weekly) {
      $dataFile = array();

  while (!feof($weekly)) {
      $line=fgets($weekly);
      //add to array
      $dataFile[]=$line; //pitting file into an array
  }
  fclose($weekly);  //closing file
  return $dataFile; //returning the array
}

function band ($datafile) { 
//function for counting data from each line of the array from 1st function
    $totalBand = 0;
    foreach ($datafile as $lines) {
        $pieces = explode(" ", $lines); //exploding file
        if ($totalBand > 0) { 
            $totalBand = $totalBand + $pieces [7]; 
            //extracting information from the 7th position in every line
        }
    }
    return $totalBand; // total value from the file
}

echo '<p>Total band = ' . $totalBand . 'bytes</p>';
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM