本文介绍了透明扁平化数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

阅读这个问题我得到了以下理念:用多级阵列工作时,可能有重复键,这将是不切实际的有将遍历这样的阵列,因为它是平坦的,像

Reading this question Merge and group by several arrays i got the following idea: when working with multilevel arrays, with possibly repeating keys, it would be practical to have a function that would iterate such an array as it were flat, like

foreach(flatten($deepArray) as $key => $val)....

任何想法怎样写压扁()?有没有什么标准溶液?

any ideas how to write flatten()? Is there any standard solution?

(注意,压扁()不能简单地返回,因为重复键的一个新的数组)。

(note that flatten() cannot simply return a new array because of repeating keys).

推荐答案

使用示例 RecursiveArrayIterator

$array = array( 
    0 => 'a', 
    1 => array('subA','subB',array(0 => 'subsubA', 1 => 'subsubB', 2 => array(0 => 'deepA', 1 => 'deepB'))), 
    2 => 'b', 
    3 => array('subA','subB','subC'), 
    4 => 'c' 
);

foreach (return new RecursiveIteratorIterator(new RecursiveArrayIterator($array))
         as $key => $val) {

    printf(
        '%s: %s' . "\n",
        $key, $val
    );
}

/* Output:
0: a
0: subA
1: subB
0: subsubA
1: subsubB
0: deepA
1: deepB
2: b
0: subA
1: subB
2: subC
4: c
*/


延长 RecursiveIteratorIterator 返回当前的关键堆栈

class MyRecursiveIteratorIterator extends RecursiveIteratorIterator
{
  public function key() {
    return json_encode($this->getKeyStack());
  }

  public function getKeyStack() {
    $result = array();
    for ($depth = 0, $lim = $this->getDepth(); $depth < $lim; $depth += 1) {
      $result[] = $this->getSubIterator($depth)->key();
    }
    $result[] = parent::key();
    return $result;
  }
}

foreach ($it = new MyRecursiveIteratorIterator(new RecursiveArrayIterator($array))
         as $key => $val) {

  printf('%s (%s): %s' . "\n", implode('.', $it->getKeyStack()), $key, $val);
}

/* Output:
0 ([0]): a
1.0 ([1,0]): subA
1.1 ([1,1]): subB
1.2.0 ([1,2,0]): subsubA
1.2.1 ([1,2,1]): subsubB
1.2.2.0 ([1,2,2,0]): deepA
1.2.2.1 ([1,2,2,1]): deepB
2 ([2]): b
3.0 ([3,0]): subA
3.1 ([3,1]): subB
3.2 ([3,2]): subC
4 ([4]): c
*/


还有一个版本,不使用RecursiveArrayIterator这个时候:


Yet another version, using no RecursiveArrayIterator this time:

function flatten(array $array = array(), $keyStack = array(), $result = array()) {
  foreach ($array as $key => $value) {
    $keyStack[] = $key;

    if (is_array($value)) {
      $result = flatten($value, $keyStack, $result);
    }
    else {
      $result[] = array(
        'keys' => $keyStack,
        'value' => $value
      );
    }

    array_pop($keyStack);
  }

  return $result;
}

foreach (flatten($array) as $element) {
  printf(
    '%s: %s (depth: %s)' . "\n",
    implode('.', $element['keys']),
    $element['value'],
    sizeof($element['keys'])
  );
}

/*
0: a (depth: 1)
1.0: subA (depth: 2)
1.1: subB (depth: 2)
1.2.0: subsubA (depth: 3)
1.2.1: subsubB (depth: 3)
1.2.2.0: deepA (depth: 4)
1.2.2.1: deepB (depth: 4)
2: b (depth: 1)
3.0: subA (depth: 2)
3.1: subB (depth: 2)
3.2: subC (depth: 2)
4: c (depth: 1)
*/

这篇关于透明扁平化数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!