在阵列内快速递归搜索所有索引 [英] Quick Recursive search of all indexes within an array

查看:114
本文介绍了在阵列内快速递归搜索所有索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好了,说我有一个数组如下:

  $按钮=阵列(
    mlist'=>阵列(
            '标题'=> '会员',
            HREF => $在scriptUrl。 '?行动= mlist',
            '秀'=> $语境['allow_memberlist'],
            sub_buttons'=>阵列(
                mlist_view'=>阵列(
                    '标题'=> 查看会员列表',
                    HREF => $在scriptUrl。 '?行动= mlist',
                    '秀'=>真正,
                )
                mlist_search'=>阵列(
                    '标题'=> 搜索会员,
                    HREF => $在scriptUrl。 行动= mlist; SA =搜索',
                    '秀'=>真正,
                    is_last'=>真正,
                )
            )
        )
    '家'=>阵列(
        '标题'=> '家',
        HREF => $在scriptUrl,
        '秀'=>真正,
        sub_buttons'=>阵列(
        )
        is_last'=> $语境['RIGHT_TO_LEFT'],
    )
    帮助=>阵列(
        '标题'=> '帮帮我',
        HREF => $在scriptUrl。 '?行动=救命',
        '秀'=>真正,
        sub_buttons'=>阵列(
        )
    )
);

我需要通过这个数组进行排序,并在另一个阵列返回它的所有索引作为指标,以及这些阵列的值将是标题。因此,它应该返回数组如下:

 阵列(
    mlist'=> '会员',
    mlist_view'=> 查看会员列表',
    mlist_search'=> 搜索会员,
    '家'=> '家',
    帮助=> '帮帮我',
);

如何才能实现这一容易实现?基本上,需要每个数组的关键如果指定一个标题,需要另一个数组中填充两个。


解决方案

  

如何才能实现这一容易实现?



  1. 初始化一个空,新的数组

  2. 的foreach的 $按钮数组键和值

    1. 从数据中抽取的标题

    2. 设置新的数组中的关键的标题


  3. 完成的。

编辑:如果<一个href=\"http://stackoverflow.com/questions/7588482/quick-recursive-search-of-all-indexes-within-an-array/7589055#7589055\">a递归迭代器阵列捕获太多(识别元素作为孩子,而他们没有 - 只是被部分的其他的数组),而你不想写递归迭代器类的一个扩展,通过所有的孩子踩着可以用一些像这样的手写迭代器来解决:

  $指数=阵列();
$ childKey ='sub_buttons';
$迭代器= $按钮;
而(名单($键,$项目)=每($迭代器))
{
    array_shift($迭代器);
    $指数[$关键] = $项目['标题'];
    $儿童=使用isset($项目[$ childKey])? $项目[$ childKey]:假的;
    如果($子女)$迭代器= $子女+ $迭代器;
}

这迭代器是意识到孩子的关键,因此,如果有一些具体只会遍历孩子的。你可以(倒数第一的孩子,孩子们)通过改变顺序控制顺序:

 如果($子女)$迭代器= $子女+ $迭代器;
- 要么 -
如果($子女)$迭代器+ = $儿童;

Ok, so say I have an array as follows:

$buttons = array(
    'mlist' => array(
            'title' => 'Members',
            'href' => $scripturl . '?action=mlist',
            'show' => $context['allow_memberlist'],
            'sub_buttons' => array(
                'mlist_view' => array(
                    'title' => 'View the Member List',
                    'href' => $scripturl . '?action=mlist',
                    'show' => true,
                ),
                'mlist_search' => array(
                    'title' => 'Search for Members',
                    'href' => $scripturl . '?action=mlist;sa=search',
                    'show' => true,
                    'is_last' => true,
                ),
            ),
        ),
    'home' => array(
        'title' => 'Home',
        'href' => $scripturl,
        'show' => true,
        'sub_buttons' => array(
        ),
        'is_last' => $context['right_to_left'],
    ),
    'help' => array(
        'title' => 'Help',
        'href' => $scripturl . '?action=help',
        'show' => true,
        'sub_buttons' => array(
        ),
    ),
);

I need to sort through this array and return all indexes of it in another array as an index, and the values of these arrays will be the title. So it should return an array as follows:

array(
    'mlist' => 'Members',
    'mlist_view' => 'View the Member List',
    'mlist_search' => 'Search for Members',
    'home' => 'Home',
    'help' => 'Help',
);

How can this be achieved easily? Basically, need the key of each array if a title is specified and need to populate both within another array.

解决方案

How can this be achieved easily?

  1. initialize an empty, new array
  2. foreach the $buttons array with key and value

    1. extract title from value
    2. set the key in the new array with the title

  3. done.

Edit: In case a recursive array iterator catches too much (identifying elements as children while they are not - just being some other array), and you don't want to write an extension of the recursive iterator class, stepping through all children can be solved with some "hand written" iterator like this:

$index = array();
$childKey = 'sub_buttons';
$iterator = $buttons;
while(list($key, $item) = each($iterator))
{
    array_shift($iterator);
    $index[$key] = $item['title'];
    $children = isset($item[$childKey]) ? $item[$childKey] : false;
    if ($children) $iterator = $children + $iterator;
}

This iterator is aware of the child key, so it will only iterate over childs if there are some concrete. You can control the order (children first, children last) by changing the order:

if ($children) $iterator = $children + $iterator;
- or - 
if ($children) $iterator += $children;

这篇关于在阵列内快速递归搜索所有索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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