为什么这段 PHP 代码不输出属性值?
我正在尝试在上传后导入制表符分隔的文件。其核心内容是通过以下函数完成的。我正在尝试构建一个类实例数组。代码如下:
导入函数
$AddedProducts;
function importList($filename)
{
global $AddedProducts;
$AddedProducts=array();
$fileHandle = fopen($filename, "r");
$currentProduct = new productImport();
$line=fgets($fileHandle); $line=fgets($fileHandle); //throw away top 2 lines
echo '<hr>';
while(true)
{
$line = fgets($fileHandle);
if($line == null) break;
$cells=explode(' ', $line);
$i=0;
foreach($currentProduct as $ProductProperty)
{
if(isset($cells[$i]))
{
$ProductProperty = $cells[$i];
echo $i . '. ' . $cells[$i] . "<br>";
}
else return false;
$i++;
}
echo "<hr>";
$AddedProducts[]=$currentProduct;
}
fclose($fileHandle);
return true;
}
数组输出
<?
$i=0;
foreach($AddedProducts as $AddedProduct)
{
$i++;
echo "<hr>" . $i . "<br>";
foreach($AddedProduct as $key=>$value)
{
echo $key . ' = ' . $value . '<br>';
}
}
?>
已知信息细分
最终数组长度/大小是正确的。 (应该是文件中的行 - 2)
productImport 类中有多少属性并不特别重要,只要它等于文件中每行的相同数量的选项卡即可读。
importList 函数回显 $cells[$i] 的正确值,这些值与我在数组输出中缺少的值相同。
问题似乎是未将值分配给属性或未读取属性。我不确定为什么会出现这种情况,但我认为这是因为 PHP 不是我的主要语言,并且 foreach 循环可能很明显;)
我正在使用 PHP v5.2.6
这段代码有什么问题吗?
答案:
foreach($currentProduct as $ProductProperty) becomes
foreach($currentProduct as &$ProductProperty)
I am trying to import a tab delimited file after upload. The meat of this is done with the following function. I'm trying to build an array of class instances. The code follows:
Import Function
$AddedProducts;
function importList($filename)
{
global $AddedProducts;
$AddedProducts=array();
$fileHandle = fopen($filename, "r");
$currentProduct = new productImport();
$line=fgets($fileHandle); $line=fgets($fileHandle); //throw away top 2 lines
echo '<hr>';
while(true)
{
$line = fgets($fileHandle);
if($line == null) break;
$cells=explode(' ', $line);
$i=0;
foreach($currentProduct as $ProductProperty)
{
if(isset($cells[$i]))
{
$ProductProperty = $cells[$i];
echo $i . '. ' . $cells[$i] . "<br>";
}
else return false;
$i++;
}
echo "<hr>";
$AddedProducts[]=$currentProduct;
}
fclose($fileHandle);
return true;
}
Array Output
<?
$i=0;
foreach($AddedProducts as $AddedProduct)
{
$i++;
echo "<hr>" . $i . "<br>";
foreach($AddedProduct as $key=>$value)
{
echo $key . ' = ' . $value . '<br>';
}
}
?>
Breakdown of Known Info
The final array length/size is correct. (Should be lines in file - 2)
It doesn't particularly matter how many properties are in the productImport class so long as it equates to the same number of tabs per line in the file being read.
importList function echos proper values for $cells[$i] which are the same values I'm missing in the array output.
The problem seems to be either the values aren't being assigned to the properties or the properties are not being read. I'm not sure of why either would be the case but I assume it is because PHP is not my primary language and likely something obvious about the foreach loops ;)
I'm using PHP v5.2.6
What's wrong with this code?
Answer:
foreach($currentProduct as $ProductProperty) becomes
foreach($currentProduct as &$ProductProperty)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为问题出在这一部分:
根据 php 手册,
除非引用了该数组,否则 foreach 将对指定数组的副本进行操作,而不是对数组本身进行操作。
因此您分配的值将在循环后被丢弃。编辑: 除此之外,您正在循环访问对象属性,尽管 手册没有明确说明它,看来你需要
foreach($class as $key => $value)
而不仅仅是foreach($class作为$值)
I think the problem is in this section:
According to the php manual,
Unless the array is referenced, foreach operates on a copy of the specified array and not the array itself.
so the value you assign is discarded after the loop.Edit: Apart from that, you are looping through object properties and although the manual does not explicitly state it, it seems you need
foreach($class as $key => $value)
instead of justforeach($class as $value)
在 foreach 循环中,分配的变量(例如
$ProductProperty
)不是引用,因此它们实际上不会影响循环之外的任何内容。即
$ProductProperty = $cells[$i]
仅影响当前迭代。In your foreach loops, the assigned variables such as
$ProductProperty
are not references, therefore they will not actually affect anything outside the loop.i.e.
$ProductProperty = $cells[$i]
only affects the current iteration.除了其他人所说的之外,您似乎每次都尝试将属性数据插入到同一个对象中,因为您没有在循环中创建任何新的 ProductImport 实例。
In addition to what the others are saying, it seems that you are attempting to insert property data to the same object every time, since you're not creating any new productImport instances in the loop.