改变关联数组中键的位置

时间:2011-06-22 22:52:27

标签: arrays sorting associative-array key

我正在创建一个表单生成器,它接受一些配置数组并构建html。例如:我有以下关联数组来描述字段的显示方式:

$fieldSpec = array(
    'nome' => array(
        'label' => 'Nome',
    ),
    'descricao' => array(
        'label' => 'Descrição',
    ),
    'preco' => array(
        'label' => 'Preço',
    ),
);

问题:我想轻松地重新定位钥匙,而不必切割和切割。一直粘贴。所以我想问一个人是否知道一个功能或类来管理这个;它必须是这样的:

ChangeKeyPosition(&$fieldSpec,'preco','up',2);

// that would produce:
$fieldSpec = array(
    'preco' => array( # <- notice this element came 2 levels up
            'label' => 'Preço',
        ),
    'nome' => array(
        'label' => 'Nome',
    ),
    'descricao' => array(
        'label' => 'Descrição',
    ),

);

我希望拥有的其他可能功能:

ChangeKeyPosition(&$fieldSpec,'descricao','down',1);
// send element "preco" to the top
ChangeKeyPosition(&$fieldSpec,'preco','top');
// send element "nome" to the end of the array
ChangeKeyPosition(&$fieldSpec,'nome','bottom');
// place "nome" before "preco"
ChangeKeyPosition(&$fieldSpec,'nome','before','preco');
// place "nome" after "descricao"
ChangeKeyPosition(&$fieldSpec,'nome','after','descricao');
// place "preco" between "nome" and "descricao"
ChangeKeyPosition(&$fieldSpec,'preco','between',array('nome','descricao'));

我甚至更喜欢使用包装数组引用的对象,如下所示:

$man = new KeyPositionManager(&$fieldSpec);
$man->up('preco',2);
$man->after('nome','descricao');
etc...

更新 所以我最终创建了一组函数来完成我的要求。它可能对其他人有用,也可能没用:

function array_key_move(&$oarr, $key, $move)
{
    $keys = array_keys($oarr);
    $opos = array_search($key,$keys);
    if($opos===false) return;
    $npos = $opos + $move + ($move > 0 ? +0.1 : -0.1);
    $keys["$npos"] = $key;
    unset($keys[$opos]);
    ksort($keys);
    foreach($keys as $k)
    {
        $narr[$k] = $oarr[$k];
    }
    $oarr = $narr;
}

function array_key_set_at(&$oarr, $tkey, $nkey, $nval, $before = true)
{
    $tpos = array_search($tkey,array_keys($oarr));
    if($tpos===false) return;
    $narr = array();
    $i = 0;
    foreach($oarr as $k => $v)
    {
        if($tpos==$i)
        {
            $before AND $narr[$nkey] = $nval;
            $narr[$k] = $v;
            $before OR $narr[$nkey] = $nval;
        } else $narr[$k] = $v;
        $i++;
    }
    $oarr = $narr;
}

function array_key_move_before(&$oarr, $key_element, $key_target)
{
    $copy_element = $oarr[$key_element];
    unset($oarr[$key_element]);
    array_key_set_at($oarr, $key_target, $key_element, $copy_element, TRUE);
}

function array_key_move_after(&$oarr, $key_element, $key_target)
{
    $copy_element = $oarr[$key_element];
    unset($oarr[$key_element]);
    array_key_set_at($oarr, $key_target, $key_element, $copy_element, FALSE);
}

function array_key_move_start(&$oarr, $key)
{
    if(!isset($oarr[$key])) return;
    $narr[$key] = $oarr[$key];
    foreach($oarr as $k => $v)
    {
        $k!=$key AND $narr[$k]=$v;
    }
    $oarr = $narr;
}

function array_key_move_end(&$oarr, $key)
{
    if(!isset($oarr[$key])) return;
    foreach($oarr as $k => $v)
    {
        $k!=$key AND $narr[$k]=$v;
    }
    $narr[$key] = $oarr[$key];
    $oarr = $narr;
}

function array_key_set_start(&$oarr,$key,$val)
{
    $oarr[$key] = $val;
    array_key_move_start($oarr,$key);
}


function array_key_set_end(&$oarr,$key,$val)
{
    $oarr[$key]=$val; # :-)
}

function array_key_switch2(&$oarr,$ka,$kb)
{
    $keys = array_keys($oarr);
    if(($kapos = array_search($ka, $keys))===false) return;
    if(($kbpos = array_search($kb, $keys))===false) return;
    $i = 0;
    foreach($oarr as $k => $v)
    {
        if($i==$kapos)
            $narr[$kb] = $oarr[$ka];
        elseif($i==$kbpos)
            $narr[$ka] = $oarr[$kb];
        else
            $narr[$k] = $v;
        $i++;
    }
    $oarr = $narr;
}

示例:

array_key_move($ ARR, 'F', - 3);

Array
(
    [a] => 1
    [b] => 2
    [f] => 6 # <- "f"
    [c] => 3 #
    [d] => 4 #
    [e] => 5
)

array_key_move($ ARR, 'A',+ 1);

Array
(
    [b] => 2 #
    [a] => 1 # <- "a"
    [f] => 6
    [c] => 3
    [d] => 4
    [e] => 5
)

array_key_move_after($ ARR, 'B', 'E');

Array
(
    [a] => 1
    [f] => 6
    [c] => 3
    [d] => 4
    [e] => 5
    [b] => 2 # <- "b" after "e"
)

array_key_move_start($ ARR, 'B');

Array
(
    [b] => 2 # <- "b" at start
    [a] => 1
    [f] => 6
    [c] => 3
    [d] => 4
    [e] => 5
)

array_key_set_start($ ARR, 'R',7);

Array
(
    [r] => 7 # <- prepended
    [b] => 2
    [a] => 1
    [f] => 6
    [c] => 3
    [d] => 4
    [e] => 5
)

* obs:上面不是array_unshift的别名 例如:array_unshift($ arr,array('r'=&gt; 7)); 会产生:*

Array
(
    [0] => Array
        (
            [r] => 7
        )

    [b] => 2
    [a] => 1
    [f] => 6
    [c] => 3
    [d] => 4
    [e] => 5
)

最后

array_key_switch2($ ARR, 'R', 'E');

Array
(
    [e] => 7 # <-
    [b] => 2
    [a] => 1
    [f] => 6
    [c] => 3
    [d] => 4
    [r] => 5 # <-
)

0 个答案:

没有答案