在 PHP 中使用迭代器时,您可以使用 iterator_to_array 函数来提取迭代结果的数组。例如,假设您有以下 ArrayObject:
$array_object = new ArrayObject(array(
array('1', '2', '3', '4'),
array('5', '6', '7', '8'),
array('9', '10', '11', '12'),
));
如你所见,它的存储是一个二维数组。
我们可以创建一个 FilterOperator 只接受它的第一项(我知道使用 LimitIterator 会更好,它只是作为示例目的):
class myFilterIterator extends FilterIterator
{
public function accept()
{
return ($this->key() === 0);
}
}
$filter_iterator = new myFilterIterator(new ArrayIterator($array_object));
现在,如果我这样做:
print_r(iterator_to_array($filter_iterator));
如果我手动循环运算符,我会得到我能得到的数组:
Array ( [0] => Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 ) )
但是现在如果我想使用 RecursiveFilterIterator 怎么办?假设我有:
class myRecursiveFilterIterator extends RecursiveFilterIterator
{
public function accept()
{
return ($this->hasChildren() || $this->key() === 0);
}
}
$recursive_filter_iterator = new myRecursiveFilterIterator(new RecursiveArrayIterator($array_object));
如您所见,对于父数组中包含的每个数组,这将只接受键 0。因此,如果我递归迭代它,它就会起作用:
foreach (new RecursiveIteratorIterator($recursive_filter_iterator) as $value) {
print_r($value);
echo '<br />';
}
结果:
1
5
9
但是,我怎样才能快速得到数组 array(array(1), array(5), array(9)) ?
如果我这样做:
print_r(iterator_to_array($recursive_filter_iterator));
或
print_r(iterator_to_array($recursive_filter_iterator->getInnerIterator()));
或
$it = new RecursiveIteratorIterator($recursive_filter_iterator);
print_r(iterator_to_array($it->getInnerIterator()));
我得到整个原始数组:
Array ( [0] => Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 ) [1] => Array ( [0] => 5 [1] => 6 [2] => 7 [3] => 8 ) [2] => Array ( [0] => 9 [1] => 10 [2] => 11 [3] => 12 ) )
如果我这样做:
print_r(iterator_to_array(new RecursiveIteratorIterator($recursive_filter_iterator)));
我只得到第一个项目:
Array ( [0] => 9 )
如果我这样做:
print_r(iterator_to_array(new RecursiveIteratorIterator($recursive_filter_iterator->getInnerIterator())));
我得到父数组中的最后一项,但键为 0:
Array ( [0] => 9 [1] => 10 [2] => 11 [3] => 12 )
我需要的是获取数组:
Array ( [0] => Array ( [0] => 1 ) [1] => Array ( [0] => 5 ) [2] => Array ( [0] => 9 ) )
我知道我可以让它手动循环,但我想知道是否有直接的方法,比如在 iterator_to_array 中用于非递归迭代器。当然,我对 PHP 中的递归迭代器有一些不了解的地方,但它的文档在这方面确实很糟糕。
非常感谢。
最佳答案
还不完全清楚你真正想做什么,但下面采用了一个 RecursiveArrayIterator(注意:ArrayObject 不是递归迭代器)并使用 iterator_to_array() 获取所需的结果数组。
class FirstOnlyRecursiveArrayIterator extends ParentIterator {
public function __construct(RecursiveArrayIterator $it) {
parent::__construct($it);
}
public function current() {
$children = parent::current();
return array_slice($children, 0, 1);
}
}
$array_it = new RecursiveArrayIterator(array(
array('1', '2', '3', '4'),
array('5', '6', '7', '8'),
array('9', '10', '11', '12'),
));
$filter_iterator = new RecursiveIteratorIterator(
new FirstOnlyRecursiveArrayIterator($array_it),
RecursiveIteratorIterator::SELF_FIRST);
print_r(iterator_to_array($filter_iterator));
关于php - 一种用于递归迭代器的 iterator_to_array 以获得二维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9112010/
我怎样才能完成http://php.net/manual/en/function.call-user-func-array.php在ruby中?所以我可以这样做:classAppdeffoo(a,b)putsa+benddefbarargs=[1,2]App.send(:foo,args)#doesn'tworkApp.send(:foo,args[0],args[1])#doeswork,butdoesnotscaleendend 最佳答案 尝试分解数组App.send(:foo,*args)
大约一年前,我决定确保每个包含非唯一文本的Flash通知都将从模块中的方法中获取文本。我这样做的最初原因是为了避免一遍又一遍地输入相同的字符串。如果我想更改措辞,我可以在一个地方轻松完成,而且一遍又一遍地重复同一件事而出现拼写错误的可能性也会降低。我最终得到的是这样的:moduleMessagesdefformat_error_messages(errors)errors.map{|attribute,message|"Error:#{attribute.to_s.titleize}#{message}."}enddeferror_message_could_not_find(obje
通过rubykoans.com,我在about_array_assignment.rb中遇到了这两段代码你怎么知道第一个是非并行赋值,第二个是一个变量的并行赋值?在我看来,除了命名差异之外,代码几乎完全相同。4deftest_non_parallel_assignment5names=["John","Smith"]6assert_equal["John","Smith"],names7end45deftest_parallel_assignment_with_one_variable46first_name,=["John","Smith"]47assert_equal'John
从给定URL下载文件并立即将其上传到AmazonS3的更直接的方法是什么(+将有关文件的一些信息保存到数据库中,例如名称、大小等)?现在,我既不使用Paperclip,也不使用Carrierwave。谢谢 最佳答案 简单明了:require'open-uri'require's3'amazon=S3::Service.new(access_key_id:'KEY',secret_access_key:'KEY')bucket=amazon.buckets.find('image_storage')url='http://www.ex
我正在使用Sequel构建一个愿望list系统。我有一个wishlists和itemstable和一个items_wishlists连接表(该名称是续集选择的名称)。items_wishlists表还有一个用于facebookid的额外列(因此我可以存储opengraph操作),这是一个NOTNULL列。我还有Wishlist和Item具有续集many_to_many关联的模型已建立。Wishlist类也有:selectmany_to_many关联的选项设置为select:[:items.*,:items_wishlists__facebook_action_id].有没有一种方法可以
我知道您通常应该在Rails中使用新建/创建和编辑/更新之间的链接,但我有一个情况需要其他东西。无论如何我可以实现同样的连接吗?我有一个模型表单,我希望它发布数据(类似于新View如何发布到创建操作)。这是我的表格prohibitedthisjobfrombeingsaved: 最佳答案 使用:url选项。=form_for@job,:url=>company_path,:html=>{:method=>:post/:put} 关于ruby-on-rails-rails:Howtomak
我试图在索引页中创建一个超链接,但它没有显示,也没有给出任何错误。这是我的index.html.erb代码。ListingarticlesTitleTextssss我检查了我的路线,我认为它们也没有问题。PrefixVerbURIPatternController#Actionwelcome_indexGET/welcome/index(.:format)welcome#indexarticlesGET/articles(.:format)articles#indexPOST/articles(.:format)articles#createnew_articleGET/article
我在pry中定义了一个函数:to_s,但我无法调用它。这个方法去哪里了,怎么调用?pry(main)>defto_spry(main)*'hello'pry(main)*endpry(main)>to_s=>"main"我的ruby版本是2.1.2看了一些答案和搜索后,我认为我得到了正确的答案:这个方法用在什么地方?在irb或pry中定义方法时,会转到Object.instance_methods[1]pry(main)>defto_s[1]pry(main)*'hello'[1]pry(main)*end=>:to_s[2]pry(main)>defhello[2]pry(main)
我已经在Sinatra上创建了应用程序,它代表了一个简单的API。我想在生产和开发上进行部署。我想在部署时选择,是开发还是生产,一些方法的逻辑应该改变,这取决于部署类型。是否有任何想法,如何完成以及解决此问题的一些示例。例子:我有代码get'/api/test'doreturn"Itisdev"end但是在部署到生产环境之后我想在运行/api/test之后看到ItisPROD如何实现? 最佳答案 根据SinatraDocumentation:EnvironmentscanbesetthroughtheRACK_ENVenvironm
我克隆了一个rails仓库,我现在正尝试捆绑安装背景:OSXElCapitanruby2.2.3p173(2015-08-18修订版51636)[x86_64-darwin15]rails-v在您的Gemfile中列出的或native可用的任何gem源中找不到gem'pg(>=0)ruby'。运行bundleinstall以安装缺少的gem。bundleinstallFetchinggemmetadatafromhttps://rubygems.org/............Fetchingversionmetadatafromhttps://rubygems.org/...Fe