声明空数组,填充和迭代麻烦 - PHP
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1946713/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Declare empty array, populate and iterate troubles - PHP
提问by Eddie
I wrote a script to read the Netflix catalog and populate my DB. Everything worked fine as a web script(except for timeouts) so I shifted to calling directly from the console.
我编写了一个脚本来读取 Netflix 目录并填充我的数据库。 作为 Web 脚本,一切正常(超时除外),因此我转而直接从控制台调用。
I noticed a few oddities, like __construct() no longer being called (but that was easily remedied using the class name as a function.
我注意到一些奇怪的地方,比如 __construct() 不再被调用(但是使用类名作为函数很容易补救。
Now I can't get my arrays to work as before, here's the general idea.
现在我不能让我的数组像以前一样工作,这是一般的想法。
(Actually I have tried a few combinations, so I'll share them all)
(其实我已经试过几种组合了,所以我把它们都分享出来)
1 - this worked in the web script version just fine, no longer works calling from console
1 - 这在 web 脚本版本中工作得很好,不再工作从控制台调用
//declare empty
var $genreArray=array();
//later I add values one at a time as the XML is parsed
array_push($this->genreArray,$attrs['term']);
//after I have parsed an entire "title" element, I iterate the array
foreach ($this->genreArray as $value) {
// never gets called - array seen as empty
$this->db->linkGenre($value,$this->title_uid);
}
2 - so I tried the PHP manual; recommendation - nothing
2 - 所以我尝试了 PHP 手册;推荐 - 没什么
//declare empty
var $genreArray=array();
//later I add values one at a time as the XML is parsed
$this->genreArray[]=$attrs['term'];
//after I have parsed an entire "title" element, I iterate the array
foreach ($this->genreArray as $value) {
// never gets called - array seen as empty
$this->db->linkGenre($value,$this->title_uid);
}
3 - so finally I tried manually tracking the index
3 - 所以最后我尝试手动跟踪索引
//declare empty array
var $genreArray=array();
var $gi=0;
//later I add values one at a time as the XML is parsed
$this->genreArray[$this->gi++]=$attrs['term'];
//after I have parsed an entire "title" element, I iterate the array
foreach ($this->genreArray as $value) {
// never gets called - array seen as empty
$this->db->linkGenre($value,$this->title_uid);
}
So I am totally stumped now.
所以我现在完全被难住了。
Has anyone declared empty arrays and populated via console?
有没有人声明过空数组并通过控制台填充?
(All 3 of these work via the web - so I need a console expert here)
(所有这三个都通过网络工作 - 所以我需要一个控制台专家在这里)
Thanks for the support, here are the additional details requested;
感谢您的支持,这是要求的其他详细信息;
php -v
php -v
PHP 4.4.9 (cli) (built: Sep 17 2008 12:02:18) Copyright (c) 1997-2008 The PHP Group Zend Engine v1.3.0, Copyright (c) 1998-2004 Zend Technologies with Zend Extension Manager v1.2.2, Copyright (c) 2003-2007, by Zend Technologies with Zend Optimizer v3.3.3, Copyright (c) 1998-2007, by Zend Technologies
PHP 4.4.9 (cli)(构建时间:2008 年 9 月 17 日 12:02:18) 版权所有 (c) 1997-2008 The PHP Group Zend Engine v1.3.0,版权所有 (c) 1998-2004 Zend Technologies with Zend Extension Manager v1。 2.2,版权 (c) 2003-2007,由 Zend Technologies 和 Zend Optimizer v3.3.3,版权 (c) 1998-2007,由 Zend Technologies
Each snippet was tried in a separate run. What details about the class are you interested in?
每个片段都在单独的运行中进行了尝试。您对课程的哪些细节感兴趣?
I have used echo statements to verify that the code is being called as expected. And if I hit the script through a URL everything kicks off fine (for the first few thousand records until the timeout).
我使用 echo 语句来验证代码是否按预期调用。如果我通过 URL 访问脚本,一切都会正常启动(对于前几千条记录,直到超时)。
No errors are being thrown, I even tried adding...
没有抛出错误,我什至尝试添加...
error_reporting(E_ALL);
ini_set('display_errors', true);
采纳答案by Ben James
I noticed a few oddities, like __construct() no longer being called (but that was easily remidied using the class name as a function.
我注意到一些奇怪的地方,比如 __construct() 不再被调用(但是使用类名作为函数很容易纠正。
It sounds like your command-line and web server PHP are different versions. Is your CLI PHP 4? The function phpversion()will tell you the version in both CLI and web.
听起来您的命令行和 Web 服务器 PHP 是不同的版本。你的 CLI 是 PHP 4 吗?该函数phpversion()将在 CLI 和 Web 中告诉您版本。
Have you checked your error log? You didn't mention it, but that would be the first place to look.
你检查过你的错误日志吗?你没有提到它,但那将是第一个看的地方。
回答by Anti Veeranna
I noticed a few oddities, like __construct() no longer being called (but that was easily remidied using the class name as a function.
我注意到一些奇怪的地方,比如 __construct() 不再被调用(但是使用类名作为函数很容易纠正。
This seems weird. What PHP version were you using in the web script and what version in the console?
这看起来很奇怪。您在 Web 脚本中使用的是什么 PHP 版本,在控制台中使用的是什么版本?
Typing php -v will tell you the version.
输入 php -v 会告诉你版本。
回答by Alix Axel
To avoid the non-CLI timeout you can do something like this:
为了避免非 CLI 超时,您可以执行以下操作:
set_time_limit(0);
You seem to be using varinstead of publicfor the class property declarations, that together with the fact that you can't use __construct()makes me guess you're using PHP 4, is that right?
您似乎正在使用var而不是public用于类属性声明,再加上您不能使用的事实__construct()让我猜您正在使用 PHP 4,对吗?
Where is $attrs['term']declared?
在哪里$attrs['term']声明?
Try doing var_dump($attrs['term']);to see what comes up.
试着做var_dump($attrs['term']);看看会发生什么。
One other thing that doesn't smell right is the following lines:
另一件闻起来不太对劲的事情是以下几行:
array_push($this->genreArray,$attrs['term']);
$this->genreArray[]=$attrs['term'];
$this->genreArray[$this->gi++]=$attrs['term'];
$attrs['term']seems to be an array, but you aren't looping through it. Unless those lines are in a separate method of it's own I think something is wrong there.
$attrs['term']似乎是一个数组,但您没有遍历它。除非这些行在它自己的单独方法中,否则我认为那里有问题。
Also, is the code you provided us part of a class? Just to make sure..
另外,您提供给我们的代码是课程的一部分吗?只想确认一下..
回答by Jon
Try removing varfrom var $genreArray=array();
尝试var从var $genreArray=array();
I had the same problem and removing varfixed it.
我有同样的问题并删除var修复它。

