移动多维数组中的元素。 [英] Move elements in a multidimensional array.

查看:101
本文介绍了移动多维数组中的元素。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有父母和孩子的多维数组,我想改变数组键

它看起来是这样的:

 阵列

    [0] => stdClass的对象
    (
        [ID] => 108
        [PARENT_ID] => 0
        [儿童] =>排列
        (
            [0] => stdClass的对象
            (
                 [ID] => 71
                 [PARENT_ID] => 108
                 [儿童] =>排列
                 (
                     [0] => stdClass的对象
                     (
                         [ID] => 107
                         [PARENT_ID] => 71
                         [儿童] =>排列
                         (
                             [0] => stdClass的对象
                             (
                                 [ID] => 78
                                 [PARENT_ID] => 107
                             )
                         )
                    )
                )
            )
        )
    )    [1] => stdClass的对象
    (
        [ID] => 82
        [PARENT_ID] => 0
    )    [2] => stdClass的对象
    (
        [ID] => 84
        [PARENT_ID] => 0
    )    [3] => stdClass的对象
    (
        [ID] => 88
        [PARENT_ID] => 0
    )    [4] => stdClass的对象
    (
        [ID] => 92
        [PARENT_ID] => 0
    )

我要移动,例如,与阵列下方的ID 108的阵列[ID] => 88的问题是一个数组移动到新的位置,而且还移动子阵列


解决方案

 函数array_remove($数组$指数){
    array_splice($数组$指数,1,阵列());
    返回$阵列;
}功能array_insert($数组,$元,$指数){
    array_splice($数组$指数,0阵列($元素));
    返回$阵列;
}功能ARRAY_MOVE($阵列,从$,$到){
    $元= $阵列[从$]。
    $阵列= array_remove($阵列,从$);
    $阵列= array_insert($数组,$元,$至 - 1);
    返回$阵列;
}$阵列= ARRAY_MOVE($阵列,0,4);
的print_r($数组);

I have a multidimensional array with parents and childs and i want to change the array keys

It looks like this:

Array
(
    [0] => stdClass Object
    (
        [id] => 108
        [parent_id] => 0
        [children] => Array
        (
            [0] => stdClass Object
            (
                 [id] => 71
                 [parent_id] => 108
                 [children] => Array
                 (
                     [0] => stdClass Object
                     (
                         [id] => 107
                         [parent_id] => 71
                         [children] => Array
                         (
                             [0] => stdClass Object
                             (
                                 [id] => 78
                                 [parent_id] => 107
                             )
                         )
                    )
                )
            )
        )
    )

    [1] => stdClass Object
    (
        [id] => 82
        [parent_id] => 0
    )

    [2] => stdClass Object
    (
        [id] => 84
        [parent_id] => 0
    )

    [3] => stdClass Object
    (
        [id] => 88
        [parent_id] => 0
    )

    [4] => stdClass Object
    (
        [id] => 92
        [parent_id] => 0
    )
)

I want to move, for example, the array with the id 108 below the array [id] => 88. The problem is to move an array to a new position but moving also the child arrays.

解决方案

function array_remove($array, $index) {
    array_splice($array, $index, 1, array());
    return $array;
}

function array_insert($array, $element, $index) {
    array_splice($array, $index, 0, array($element));
    return $array;
}

function array_move($array, $from, $to) {
    $element = $array[$from];
    $array = array_remove($array, $from);
    $array = array_insert($array, $element, $to - 1);
    return $array;
}

$array = array_move($array, 0, 4);
print_r($array);

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

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