Php 将数组传递给另一个函数
嘿,伙计们,我的一个实验有一些问题,基本上我正在尝试读取一个文件,然后将其放入一个数组,并希望在其他函数中使用该数组Php 将数组传递给另一个函数,php,arrays,Php,Arrays,嘿,伙计们,我的一个实验有一些问题,基本上我正在尝试读取一个文件,然后将其放入一个数组,并希望在其他函数中使用该数组 $totalBand = 0; $weekly = fopen('filepath', 'r'); //opening my file handler($weekly); //Calling my function function handler ($weekly) { $dataFile = array
$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>';
$totalBand=0;
$weekly=fopen('filepath','r')//打开我的文件
处理人(每周)//调用我的函数
函数处理程序($weekly){
$dataFile=array();
而(!feof($weekly)){
$line=fgets(每周);
//添加到数组
$dataFile[]=$line;//将文件放入数组
}
fclose($weekly);//关闭文件
return$dataFile;//返回数组
}
函数带($datafile){
//函数,用于从第一个函数开始计算数组各行的数据
$totalBand=0;
foreach($datafile作为$line){
$pieces=分解(“,$lines);//分解文件
如果($totalBand>0){
$totalBand=$totalBand+$pieces[7];
//从每行的第7个位置提取信息
}
}
返回$totalBand;//文件中的总值
}
回波“总频带=”$totalBand。”字节';
我没有得到任何错误,但也没有得到结果,我知道信息位于正确的位置,在文件中,我认为这是我的第一个没有完成任务的函数,即返回/传递数组
任何帮助都会很好 您没有存储来自函数处理程序的结果。或者调用函数带。需要一些类似于
$result = handler($weekly);
$totalBand = band($result);
echo '<p>Total band = ' . $totalBand . 'bytes</p>';
$result=handler($weekly);
$totalBand=波段($result);
回波“总频带=”$totalBand。”字节';
如果需要的话,你可以用链子或者做一个衬里,但是看起来很难看
可能需要继续阅读,因为看起来您正试图从全局范围访问局部函数变量。函数完成后,您不能再访问函数内部的变量,除非它们被全局声明。
<?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>';
?>
从何处调用函数带()
在fopen()
之后的$weekly
是什么?如果函数返回值,则应将其分配给变量,不是吗?$weekly指的是我正在打开的文件的位置。在本例中,它是一个.log文件。应该使用这种类型的打字错误可以理解为@metalmail先生。但是,更新了。谢谢。即使在应用了这些更改之后,它仍然没有给我一个结果,并且使用if(empty($dataFile)){echo'$var是0,empty,或者根本没有设置';}告诉我它是空的,但是我可以在没有函数的情况下让它工作得很好,所以可能是我在使用wronf函数将信息放入数组中吗。。您应该避免关闭?>以防止标记后出现任何意外的空格导致问题,但您可能遇到了这种情况。调用handler()后可能会出现var_dump($datafile)。确保它输出的是您期望的内容。