移动阵列中的一个关联数组键 [英] Move an associative array key within an array

查看:194
本文介绍了移动阵列中的一个关联数组键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎么能在一个阵列移动的副项,以便它是另一个指定的键后?

例如, $列的电流输出如下 -

  $列=阵列(
    [标题] =>标题
    [作者] =>作者
    [分类法news_type] =>新闻类型
    [日期] =>日期
    [wpseo得分=>搜索引擎优化
    [P2P-从-news_to_people] =>工作人员联系到这个故事
    [show_item_ids] => ID

一种情况是,我想移动的关键 P2P-从-news_to_people 直接后分类法news_type ,生产这种输出 -

  $列=阵列(
    [标题] =>标题
    [作者] =>作者
    [分类法news_type] =>新闻类型
    [P2P-从-news_to_people] =>工作人员联系到这个故事
    [日期] =>日期
    [wpseo得分=>搜索引擎优化
    [show_item_ids] => ID


解决方案

您可以创建一个自定义功能的特定按键后插入一个新的项目:

 函数array_insert_after($数组$ findAfter,$键,$新)
{
    $ POS =(INT)array_search($ findAfter,array_keys($阵列))+ 1;
    返回array_merge(
        array_slice($阵列,0,$ POS),
        阵列($关键=> $新)
        array_slice($数组$ POS)
    );
}

用法:

  $ ELEM = $改编['P2P  - 从 -  news_to_people']; //存储值
未设置($改编['P2P - 从 - news_to_people']); //取消设置VAR$ ARR = array_insert_after(
    $ ARR,/ *原始数组* /
    分类学news_type',/ *项目之后,ELEM应插入* /
    P2P-从-news_to_people',/ *的* ELEM关键/
    $ ELEM / *的* ELEM的价值/
);的print_r($ ARR);

输出:

 阵列

    [标题] =>标题
    [作者] =>作者
    [分类法news_type] =>新闻类型
    [P2P-从-news_to_people] =>工作人员联系到这个故事
    [日期] =>日期
    [wpseo得分=>搜索引擎优化
    [show_item_ids] => ID

演示

How can I move an associate key in an array so that it is after another specified key?

For example, the current output of $columns is as follows -

$columns = Array(
    [title] => Title
    [author] => Author
    [taxonomy-news_type] => News Types
    [date] => Date
    [wpseo-score] => SEO
    [p2p-from-news_to_people] => Staff linked to this Story
    [show_item_ids] => ID
)

One scenario is that I would like to move the key p2p-from-news_to_people to directly after taxonomy-news_type, producing this output -

$columns = Array(
    [title] => Title
    [author] => Author
    [taxonomy-news_type] => News Types
    [p2p-from-news_to_people] => Staff linked to this Story
    [date] => Date
    [wpseo-score] => SEO
    [show_item_ids] => ID
)

解决方案

You can build a custom function to insert a new item after a specific key:

function array_insert_after($array, $findAfter, $key, $new)
{
    $pos = (int) array_search($findAfter, array_keys($array)) + 1;
    return array_merge(
        array_slice($array, 0, $pos),
        array($key => $new),
        array_slice($array, $pos)
    );
}

Usage:

$elem = $arr['p2p-from-news_to_people'];  // store the value
unset($arr['p2p-from-news_to_people']);   // unset the var

$arr = array_insert_after(
    $arr,                      /* original array */
    'taxonomy-news_type',      /* item after which the elem should be inserted */
    'p2p-from-news_to_people', /* key of the elem */
    $elem                      /* the value of the elem */
);

print_r($arr);

Output:

Array
(
    [title] => Title
    [author] => Author
    [taxonomy-news_type] => News Types
    [p2p-from-news_to_people] => Staff linked to this Story
    [date] => Date
    [wpseo-score] => SEO
    [show_item_ids] => ID
)

Demo

这篇关于移动阵列中的一个关联数组键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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