如何使用递归数组迭代器来处理多维数组? [英] How do I use a recursive array iterator to process a multidimensional array?

查看:162
本文介绍了如何使用递归数组迭代器来处理多维数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试这样的工作:

I'm trying to get something like this working:

function posts_formatter (&$posts){
    foreach ($posts as $k => $v){

        if (is_array($v)){

            posts_formatter($v);

        }else{

            switch (strtolower($k)){

                # make email addresses lowercase
                case (strpos($k, 'email') !== FALSE):
                    $posts[$k] = strtolower($v);
                    break;

                # make postcodes uppercase
                case (strpos($k, 'postcode') !== FALSE):
                    $posts[$k] = strtoupper($v);
                    break;

                # capitalize certain things
                case (strpos($k, 'line1') !== FALSE):
                case (strpos($k, 'line2') !== FALSE):
                case (strpos($k, 'line3') !== FALSE):
                case (strpos($k, 'forename') !== FALSE):
                case (strpos($k, 'surname') !== FALSE):
                    $posts[$k] = capitalize($v);
                    break;
            }

        }
    }
}

它将正确地通过数组并格式化值,但我无法让它返回它们。我已经玩过从函数声明中删除& 并在最后添加一个返回但它不会做任何事情。

It will correctly go through the array and format the values but I can't get it to return them. I've played around with removing the & from the function declaration and adding a return at the end but it won't do anything.

另外,我想也许可以使用 RecursiveArrayIterator 。然而,尽管我面前有一本关于SPL迭代器的章节,但它的例子对于我能够实现我想要的东西毫无用处。我将如何实施一个?

Additionally, I'm thinking perhaps using a RecursiveArrayIterator might be the way to go. However, despite the presence of a book right in front of me with a chapter on SPL Iterators its examples are useless towards being able to achieve what I'm trying to. How would I go about implementing one?

编辑:

array (
  'user' => 
  array (
    'title' => 'Mr.',
    'forename' => 'lowercase',
    'surname' => 'name',
    'businessName' => 'some dude',
    'telephone' => '07545464646',
    'postcode' => 'wa1 6nj',
    'line1' => 'blergh road',
    'line2' => 'randomLY cApitaLIzed wOrds',
    'line3' => '',
  ),
  'email' => 'CAPITALIZED@BLERGH.com',
  'address' => 
  array (
    'postcode' => 'ab1 1ba',
    'line1' => 'test road',
    'line2' => 'testville',
    'line3' => 'testshire',
  ),
  'date' => '2010-09-30'
)


推荐答案

好的,这是一个快速的东西给你弄明白:

Ok, here is a quick something for you to figure out:

$data = array(
    'title'    => 'how to work with iterators',
    'posts' => array(
        array(
        'title'  => 'introduction to iterators',
        'email'  => 'JohnDoe@example.com'
     ), array(
        'title'  => 'extending iterators',
        'email'  => 'JaneDoe@example.com'
     )
));

主要想法是影响 Iterator 返回当前元素。迭代器是可堆叠的,因此您应该使用 RecursiveArrayIterator 并将其包装到 RecursiveIteratorIterator 中。要实现自定义功能,您可以继承 RecursiveIteratorIterator (如下所示)或使用其他迭代器来装饰 RecursiveIteratorIterator

The main idea is to influence how the Iterator returns the current element. Iterators are stackable, so you should use a RecursiveArrayIterator and wrap it into a RecursiveIteratorIterator. To achieve custom functionality, you can either subclass the RecursiveIteratorIterator (as shown below) or use additional iterators to decorate the RecursiveIteratorIterator:

class PostFormatter extends RecursiveIteratorIterator
{
    public function current()
    {
        $current = parent::current();
        switch($this->key()) {
            case 'email':
                $current = strtolower($current);
                break;
            case 'title':
                $current = ucwords($current);
                break;
            default:
                break;
        }
        return $current;
    }
}

然后你只需 foreach over iterator

Then you simply foreach over the iterator

$it = new PostFormatter(new RecursiveArrayIterator($data));
foreach($it as $key => $post) {
    echo "$key: $post", PHP_EOL;
}

并获得

title: How To Work With Iterators
title: Introduction To Iterators
email: johndoe@example.com
title: Extending Iterators
email: janedoe@example.com






你可以尝试从 iterator_to_array iterator_apply 函数中获取数组。但是,要将值重新应用于原始数组结构,您不需要迭代器:


You can try to get the array back from the with iterator_to_array or iterator_apply functions. However, to get the values reapplied to the original array structure, you dont need an iterator:

array_walk_recursive($data, function(&$val, $key) {
    switch($key) {
        case 'title': $val = ucwords($val); break;
        case 'email': $val = strtolower($val); break;
        default: break;
    }
});
print_r($data);

注意:使用PHP< 5.3

Note: exchange Lambda with Function name when using PHP<5.3

这篇关于如何使用递归数组迭代器来处理多维数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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