开发者

traversing array, which can be of multiple depth

I have following array pattern.

Array
(
[0] => Array
    (
        [label] => 2011
        [subtable] => Array
            (
                [0] => Array
                    (
                        [label] => 05
                        [subtable] => Array
                            (
                                [0] => Array
                                    (
                                        [label] => /hello-world.html
                                        [url] => http://example.com/2011/05/hello-world.html
                                    )

                                [1] => Array
                                    (
                                        [label] => /test-test-test.html
                                        [url] => http://example.com/2011/05/test-test-test.html
                                    )

                            )

                    )

                [1] => Array
                    (
                        [label] => 04
                        [subtable] => Array
                            (
                                [0] => Array
                                    (
                                        [label] => /i-have-some-problem-with-gfc-updates.html
                                        [url] => http://example.com/2011/04/i-have-some-problem-with-gfc-updates.html
                                    )

                                [1] => Array
                                    (
                                        [label] => /testing-for-google-frineds-update.html
                                        [url] => http://example.com/2011/04/testing-for-google-frineds-update.html
                                    )

                            )

                    )

                [2] => Array
                    (
                        [label] => /05
                        [url] => http://example.com/2011/05
                    )

                [3] => Array
                    (
      开发者_Go百科                  [label] => /04
                        [url] => http://example.com/2011/04
                    )

            )

    )

[1] => Array
    (
        [label] => /index
        [url] => http://example.com/
    )
)

I want to get "label" and "url" into another array like

array(
    [0] = array([label] => a [url] => http://example.com/a/)
    [1] = array([label] => b [url] => http://example.com/b/)
)

How can I achieve it using recursion, or by passing it to a function?


You may use array_walk_recursive for this:

$parsed_array = array();
function parse_array($item, $key) {
  global $parsed_array;
  if(is_array($item) && isset($item['url'])) {
    $parsed_array[] = $item;
  }
}

array_walk_recursive($your_array, 'parse_array');

Or you may implement it using a recursive function:

function parse_array($array) {
  $result = array();
  foreach($array as $item) {
    if(isset($item['subtable'])) {
      $result = array_merge($result, parse_array($item['subtable']));
    }else{
      $result[] = $item;
    }
  }
  return $result;
}

$parsed_array = parse_array($your_array);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜