从函数返回数组时出现问题 [英] Having a problem returning an array from a function

查看:85
本文介绍了从函数返回数组时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为一个类编写代码,我已经设置了从与PHP文件相同的目录中的文件加载数据的函数(使用file方法)。然后,目标是程序从该功能中获取信息,1)从用户输入的名称中搜索匹配,2)用新名称替换旧名称(由第二个函数扫描),3)将其保存回原始文件。该文件包含由另一个PHP页面输入的信息,但这确实无关紧要。问题是我编写了以下测试代码来从文件中检索信息,但它无法正常工作。


<?php

function loadData()

{

if(!$ filename = file(" employees.dat"))

{

echo(''打开文件'时出错');

}

其他

{


$ numLines = count($ filename);

for($ i = 0; $ i< $ numLines; $ i ++)

{

$ filename [$ i];

}

返回数组($ filename);

}

}


$ myFile = loadData();

echo($ myFile) ;

?>


再次,这只是一个测试代码。最终目的不是将数组打印到屏幕上,而是从函数中检索数据,以便将其传递给下一个将扫描数组的函数,以查看用户的输入是否与现有名称匹配。文件。在我使这部分正常工作后,我可以发布其余的代码。


P.S.现在的功能的结果是单词Array。打印在屏幕的左上角,没有别的。 [看起来它正在返回返回TYPE而不是数组中元素的实际值]。任何帮助都将非常感谢,因为关于类似问题的所有其他帖子似乎都与数据库有关,并且不适用。谢谢。

I''m writing code for a class where I''ve set up functions to load data from a file in the same directory as the PHP file (using the "file" method). Then, the goal is for the program to take the info from that function and, 1) search for a match from user-inputted names, 2) replace the old name (scanned by the second function) with the new name, and 3) save it back to the original file. The file contains info that was input to it by another PHP page, but that really doesn''t matter. The problem is that I wrote the following test code to retrieve the info from the file, but it''s not working properly.

<?php
function loadData()
{
if(!$filename = file("employees.dat"))
{
echo(''Error while opening file'');
}
else
{

$numLines = count($filename);
for ($i = 0; $i < $numLines; $i++)
{
$filename[$i];
}
return array($filename);
}
}

$myFile = loadData();
echo($myFile);
?>

Again, this is just a test code. The ultimate aim is not to print the array to the screen, but to retrieve the data from the function so that it can be passed on to the next function that will scan the array to see if an input from the user matches an existing name in the file. I can post the rest of the code after I get this part working correctly.

P.S. The result of the function as it stands right now, is the word "Array" printed at the top left of the screen and nothing else. [It appears that it''s returning the return TYPE instead of the actual values of the elements in the array]. Any help would be much appreciated, as all the other posts about similar issues seemed to be about databases, and not applicable. Thanks.

推荐答案

filename = file(" employees.dat"))

{

echo( ''打开文件时出错'');

}

其他

{

filename = file("employees.dat"))
{
echo(''Error while opening file'');
}
else
{


numLines = count(
numLines = count(


filename);

for(
filename);
for (


这篇关于从函数返回数组时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆