Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
371 views
in Technique[技术] by (71.8m points)

php - Transparently flatten an array

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Example usage of 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' . "
",
        $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
*/

extending RecursiveIteratorIterator to return the current key-stack

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' . "
", 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
*/

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)' . "
",
    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)
*/

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...