从另一个多维数组创建多维

时间:2014-05-15 18:08:59

标签: php arrays multidimensional-array

现在破坏我的大脑一段时间并需要你的帮助

我有一个数组如下:

$originalArray = array(
    array('id' => 1, 'sub-id' => 0),
    array('id' => 2, 'sub-id' => 0),
    array('id' => 3, 'sub-id' => 1),
    array('id' => 4, 'sub-id' => 3),
    array('id' => 5, 'sub-id' => 4),
    array('id' => 6, 'sub-id' => 0),
    array('id' => 7, 'sub-id' => 0),
    array('id' => 8, 'sub-id' => 6),
    array('id' => 9, 'sub-id' => 8),
    array('id' => 10, 'sub-id' => 8)
);

这里的逻辑是

  

如果任何元素的sub-id等于另一个元素的id,   然后数组进入父元素的sub键。即子标识1应该进入' sub' id 1和sub-id 3的元素应该放在' sub' id 3的元素

上述数组所需的输出是:

$requiredArray = array(
    array('id' => 1,'sub-id' => 0,
        'sub' => array(
            array('id' => 3,'sub-id' => 1,
                'sub' => array(
                    array('id' => 4,'sub-id' => 3,
                        'sub' => array(
                            array('id' => 5,'sub-id' => 4)
                        )
                    )
                )
            )
        )
    ),
    array('id' => 2,'sub-id' => 0),
    array('id' => 6,'sub-id' => 0,
        'sub' => array(
            array('id' => 8,'sub-id' => 6,
                'sub' => array(
                    array('id' => 9,'sub-id' => 8),
                    array('id' => 10,'sub-id' => 8)
                )
            )
        )
    ),
    array('id' => 7,'sub-id' => 0)
);

到目前为止我尝试了什么

// $original array is the array shown above
function compare_subid($a, $b)
{
    if ($a['sub-id'] == $b['sub-id']) return 0;
    return ($a['sub-id'] < $b['sub-id']) ? -1 : 1;
}

usort($originalArray, 'compare_subid');

$newArray = array();
$newArray = create_multidimensional($originalArray, $newArray);

function create_multidimensional($originalArray, $newArray = null)
{
    if ($newArray == null) $newArray = array();
    array_walk($originalArray, function ($value, $key) use (&$newArray) {
        //e($value);
        if ($value['sub-id'] == 0) {
            $newArray[] = $value;
        } else {
            foreach ($newArray as &$v) {
                if ($v['id'] == $value['sub-id']) {
                    $v['sub'] = $value;
                } else {
                    // not sure what to put here
                }
            }
        }
    });
    return $newArray;
}

有了这个,我能够实现$requiredArray的一部分,如下所示:

Array
(
    [0] => Array
        (
            [id] => 6
            [sub-id] => 0
            [sub] => Array
                (
                    [id] => 8
                    [sub-id] => 6
                )

        )

    [1] => Array
        (
            [id] => 7
            [sub-id] => 0
        )

    [2] => Array
        (
            [id] => 2
            [sub-id] => 0
        )

    [3] => Array
        (
            [id] => 1
            [sub-id] => 0
            [sub] => Array
                (
                    [id] => 3
                    [sub-id] => 1
                )

        )
)

不确定这是否是正确的使用方法,或者有更好的方法。

如果我正在做的是正确的,我无法弄清楚在我创建的create_multidimensional函数的else语句中输入了什么。

2 个答案:

答案 0 :(得分:2)

使用引用有一种简单的方法,只删除末尾有sub-id的引用:

// We need keys to be able to quickly map our assignments
foreach ($originalArray as $val) {
    $array[$val["id"]] = $val;
}

// we first assign the arrays in a non-destructive way, so that we can easily find the
// appropriate key in the array
foreach ($array as $key => $val) {
    if ($val["sub-id"] !== 0) {
        $array[$val["sub-id"]]["sub"][] = &$array[$key];
    }
}

// remove the ones from the first dimension which are somewhere deeper
foreach ($array as $key => $val) {
    if ($val["sub-id"] !== 0) {
        unset($array[$key]);
    }
}

答案 1 :(得分:2)

这就是为什么PHP中的对象很酷。

// Format all data into objects keyed by id
$input = array();
foreach ($originalArray as $el) {
  $input[ $el['id'] ] = (object)($el + array('sub' => array()));
}

$result = array();
foreach ($input as $el) {
  $sid = $el->{'sub-id'};

  // Parent object: into result root
  if ( !$sid ) {
    $result[] = $el;
  }
  // Child object: into other object
  else {
    $input[$sid]->sub[] = $el;
  }
}

print_r($result);

明显的缺点是对象使用->prop语法,这对-不起作用,所以你必须丑陋:$el->{'sub-id'}

当然结果是一堆物体。可能不是你想要的。

结果(http://3v4l.org/8VMJr):

Array
(
    [0] => stdClass Object
        (
            [id] => 1
            [sub-id] => 0
            [sub] => Array
                  (
                    [0] => stdClass Object
                        (
                            [id] => 3
                            [sub-id] => 1
                            [sub] => Array
                                (
                                    [0] => stdClass Object
                                        (
                                            [id] => 4
                                            [sub-id] => 3
                                            [sub] => Array
                                                (
                                                    [0] => stdClass Object
                                                        (
                                                            [id] => 5
                                                            [sub-id] => 4
                                                            [sub] => Array
                                                                (
                                                                )
                                                        )
                                                )
                                        )
                                )
                        )
                )
        )
    [1] => stdClass Object
        (
            [id] => 2
            [sub-id] => 0
            [sub] => Array
                (
                )
        )
    [2] => stdClass Object
        (
            [id] => 6
            [sub-id] => 0
            [sub] => Array
                (
                    [0] => stdClass Object
                        (
                            [id] => 8
                            [sub-id] => 6
                            [sub] => Array
                                (
                                    [0] => stdClass Object
                                        (
                                            [id] => 9
                                            [sub-id] => 8
                                            [sub] => Array
                                                (
                                                )
                                        )
                                    [1] => stdClass Object
                                        (
                                            [id] => 10
                                            [sub-id] => 8
                                            [sub] => Array
                                                (
                                                )
                                        )
                                )
                        )
                )
        )
    [3] => stdClass Object
        (
            [id] => 7
            [sub-id] => 0
            [sub] => Array
                (
                )
        )
)