如何使用递归数组迭代器来处理多维数组?

时间:2010-09-30 09:47:18

标签: php recursion iterator multidimensional-array

我正试图让这样的事情发挥作用:

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;
            }

        }
    }
}

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

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

编辑:

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'
)

1 个答案:

答案 0 :(得分:19)

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

$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返回current元素的方式。迭代器是可堆叠的,因此您应该使用RecursiveArrayIterator并将其包装到RecursiveIteratorIterator中。要实现自定义功能,您可以继承RecursiveIteratorIterator(如下所示)或使用其他迭代器来装饰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通过迭代器

$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_arrayiterator_apply函数重新获取数组。但是,要将值重新应用于原始数组结构,您不需要迭代器:

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

时,使用函数名交换Lambda