移动多维数组项

时间:2014-04-28 13:03:46

标签: php arrays multidimensional-array

所以我有这个数组:

 $input = array (
    1 => array (
        'TitleName' => 'Details',
        'TitleID' => 1,
        1 => array (
          'ID' => 1,
          'Name' => 'First Name'
        ),
        2 => array (
            'ID' => 2,
            'Name' => 'Last Name'
        ),
        3 => array (
            'ID' => 4,
            'Name' => 'City')
        ),
    12 => array (
        'TitleName' => 'System',
        'TitleID' => 12,
        0 => array (
          'ID' => 3,
          'Name' => 'Cpu'
        )
    )
);

我有一个数组告诉我如何订购上面的数组:

$order = array
(
    1 => array(
        0 => 1, // this is the ID in the third dimension
        1 => 4,
        2 => 2,
    ),
    12 => array (
        0 => 3
    )
);

所以关键是我将进入我的最终阵列:

Array
(
    [1] => Array
        (
            [TitleName] => Details
            [TitleID] => 1
            [1] => Array
                (
                    [ID] => 1
                    [Name] => First Name
                )
            [2] => Array
                (
                    [ID] => 4
                    [Name] => City
                )
            [3] => Array
                (
                    [ID] => 2
                    [Name] => Last Name
                )
        )

    [12] => Array
        (
            [TitleName] => System
            [TitleID] => 12
            [0] => Array
                (
                    [ID] => 3
                    [Name] => Cpu
                )
        )
)

另外,如何将数组中的项目移动到不同的父项?

我已尝试过此代码,但没有运气。

usort($array, function ($a, $b) use ($order) {
    $pos_a = array_search($a['id'], $order);
    $pos_b = array_search($b['id'], $order);
    return $pos_a - $pos_b;
});

任何想法?谢谢!

1 个答案:

答案 0 :(得分:1)

由于您的id是唯一的,因此填充两个临时数组然后遍历排序数组以创建所需的输出可能更容易。

Here's a possible solution。这里不需要array_search。

拥有两个goven数组,我们首先迭代输入,分离常见的第一级元素及其属性和子元素。

为了区分第一级元素和chiuldren的属性,我们在键上使用is_numeric(因为属性键不是数字)和is_array(只是为了确保)。

// our temporary arrays
$tmpElements = array(); 
$tmpChildren = array();

// iterate over array
foreach($input as $key => $value) {
    $tmpElementAttributes = array(); // init/reset the temporary attributes array

    // iterate over children and attributes
    foreach ($value as $subKey => $subValue) {

            // if the value is an array and the key is numeric, it is a child element
        if(is_array($subValue) && is_numeric($subKey)) {
            $tmpChildrenKey = $subValue['ID'];
            $tmpChildren[$tmpChildrenKey] = $subValue;
        }
        else { // otherwise it is an attribute
            $tmpElementAttributes[$subKey] = $subValue;
        }
    }
    $tmpElements[$key] = $tmpElementAttributes; // add the gathered attributes that define our firstLevel Element
}

所以现在我们有两个数组。一个( $tmpElements )包含所有第一个级别元素(详细信息,系统),另一个( $tmpChildren )拥有所有子元素(First Name, Last Name, Cpu, City)。对于这两个数组,我们将其id作为数组键。

现在我们遍历排序数组,将我们的子元素填入我们的$tmpElements数组中符合$order的相应第一级元素。

foreach($order as $key => $values) {
    foreach($values as $orderId) {
        $tmpElements[$key][] = $tmpChildren[$orderId];
    }
}

此处,第一个数组中的$key是我们的第一个级别元素的数组键,无论是在源/输入中还是在我们的$tmpElements中都是如此。所以我们可以用它来识别数组中的元素。

第二个foreach中的$orderId是第二级子元素ID。 所以我们用它来达到我们各自的儿童元素。 因此:$tmpElements[$key][] = $tmpChildren[$orderId];

相关问题