将多维多维PHP数组操作为一个漂亮的多维数组

时间:2014-10-20 23:06:20

标签: php arrays

我知道这被认为是一个有点基本的PHP数组问题,但我正在运行35个小时没有睡觉,我真的只需要像posibble一样快完成这个,所以我可以睡觉...对不起只是诚实!

在PHP中,我有这个变量$design_values

如果我print_r($design_values);这个ARRAY它会吐出下面显示的内容。它是“Desing”数据库记录。

在这种情况下,有2个设计记录构成01

的前2个数组键

在应用程序中,可以有任意数量的设计,从0到任意数字。

现在,在2个设计记录中,每个2个设计阵列都有24个数组键。

这些24数组键编号为023

现在在每个24个数组键下,有2个键。一个名为name,另一个名为value

我需要获取数组$design_values并创建该数组的新数组。新数组的格式应该更好,并且易于使用。

因此namevalue键应构成key => value。 New数组看起来应该更像......

当前阵列是一个完整的噩梦的原因是因为这是我从现有库中获取的格式,该库从API调用返回此数据。

如果有人可以帮我操作这个数组到所需的阵列,我将不胜感激!我一直在搞乱它2个小时没有运气。

所需的新阵列格式:

Array
(
    [0] => Array
        (
            ['assigned_user_name'] => 'Jason Administrator',
            ['modified_by_name'] => 'Jason Administrator',
            ['created_by_name'] => 'Jason Administrator',
            ['id'] => '4c5c3c08-2b14-9f9c-6cee-542c56cac7b1',
            ['date_entered'] => '2014-10-01 19:29:32',
            ....continued for all 24 record items
        ),
    [1] => Array
        (
            ['assigned_user_name'] => 'Jason Administrator',
            ['modified_by_name'] => 'Jason Administrator',
            ['created_by_name'] => 'Jason Administrator',
            ['id'] => '4c5c3c08-2b14-9f9c-6cee-542c56cac7b1',
            ['date_entered'] => '2014-10-01 19:29:32',
            ....continued for all 24 record items
        )
)

当前数据格式:

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [name] => assigned_user_name
                    [value] => Jason Administrator
                )

            [1] => Array
                (
                    [name] => modified_by_name
                    [value] => Jason Administrator
                )

            [2] => Array
                (
                    [name] => created_by_name
                    [value] => Jason Administrator
                )

            [3] => Array
                (
                    [name] => id
                    [value] => 4c5c3c08-2b14-9f9c-6cee-542c56cac7b1
                )

            [4] => Array
                (
                    [name] => name
                    [value] => test
                )

            [5] => Array
                (
                    [name] => date_entered
                    [value] => 2014-10-01 19:29:32
                )

            [6] => Array
                (
                    [name] => date_modified
                    [value] => 2014-10-01 19:29:32
                )

            [7] => Array
                (
                    [name] => modified_user_id
                    [value] => 1
                )

            [8] => Array
                (
                    [name] => created_by
                    [value] => 1
                )

            [9] => Array
                (
                    [name] => description
                    [value] => 
                )

            [10] => Array
                (
                    [name] => deleted
                    [value] => 0
                )

            [11] => Array
                (
                    [name] => assigned_user_id
                    [value] => 1
                )

            [12] => Array
                (
                    [name] => chann_channelqms_id_c
                    [value] => 
                )

            [13] => Array
                (
                    [name] => channelqms
                    [value] => 
                )

            [14] => Array
                (
                    [name] => design_name
                    [value] => 
                )

            [15] => Array
                (
                    [name] => design_number
                    [value] => 
                )

            [16] => Array
                (
                    [name] => overall_height
                    [value] => 
                )

            [17] => Array
                (
                    [name] => overall_width
                    [value] => 
                )

            [18] => Array
                (
                    [name] => show_to_customer
                    [value] => 1
                )

            [19] => Array
                (
                    [name] => uploadfile
                    [value] => 2014-09-29_21-57-50.png
                )

            [20] => Array
                (
                    [name] => nam_channelletterqms_nam_channelletterqms_designs_name
                    [value] => Test
                )

            [21] => Array
                (
                    [name] => price_c
                    [value] => 
                )

            [22] => Array
                (
                    [name] => shipping_c
                    [value] => 
                )

            [23] => Array
                (
                    [name] => totalprice_c
                    [value] => 
                )

        )

    [1] => Array
        (
            [0] => Array
                (
                    [name] => assigned_user_name
                    [value] => Jason Administrator
                )

            [1] => Array
                (
                    [name] => modified_by_name
                    [value] => Jason Administrator
                )

            [2] => Array
                (
                    [name] => created_by_name
                    [value] => Jason Administrator
                )

            [3] => Array
                (
                    [name] => id
                    [value] => 86f21f44-4b21-1826-3592-542c59e4be66
                )

            [4] => Array
                (
                    [name] => name
                    [value] => fdtgrfdhg
                )

            [5] => Array
                (
                    [name] => date_entered
                    [value] => 2014-10-01 19:41:54
                )

            [6] => Array
                (
                    [name] => date_modified
                    [value] => 2014-10-19 19:30:45
                )

            [7] => Array
                (
                    [name] => modified_user_id
                    [value] => 1
                )

            [8] => Array
                (
                    [name] => created_by
                    [value] => 1
                )

            [9] => Array
                (
                    [name] => description
                    [value] => 
                )

            [10] => Array
                (
                    [name] => deleted
                    [value] => 0
                )

            [11] => Array
                (
                    [name] => assigned_user_id
                    [value] => 1
                )

            [12] => Array
                (
                    [name] => chann_channelqms_id_c
                    [value] => 
                )

            [13] => Array
                (
                    [name] => channelqms
                    [value] => 
                )

            [14] => Array
                (
                    [name] => design_name
                    [value] => design name
                )

            [15] => Array
                (
                    [name] => design_number
                    [value] => 313
                )

            [16] => Array
                (
                    [name] => overall_height
                    [value] => 22
                )

            [17] => Array
                (
                    [name] => overall_width
                    [value] => 22
                )

            [18] => Array
                (
                    [name] => show_to_customer
                    [value] => 1
                )

            [19] => Array
                (
                    [name] => uploadfile
                    [value] => 2014-09-29_21-57-50.png
                )

            [20] => Array
                (
                    [name] => nam_channelletterqms_nam_channelletterqms_designs_name
                    [value] => Test
                )

            [21] => Array
                (
                    [name] => price_c
                    [value] => 
                )

            [22] => Array
                (
                    [name] => shipping_c
                    [value] => 
                )

            [23] => Array
                (
                    [name] => totalprice_c
                    [value] => 
                )

        )

)

2 个答案:

答案 0 :(得分:3)

如果你不能在源头改变它,那么这里是一种方式(array_column需要PHP> = 5.5.0):

foreach($design_values as $key => $values) {
    $result[$key] = array_combine(
                                  array_column($values, 'name'),
                                  array_column($values, 'value'));
}

或者可能,也更容易:

foreach($design_values as $key => $values) {
    $result[$key] = array_column($values, 'value', 'name');
}

我们使用PHP Implementation of array_column

答案 1 :(得分:2)

你应该在制作第一个数组时创建你想要的数组,但是如果你不能控制它而只是想转换那么这样的东西应该有效:

$newArray = array();

foreach($oldArray as $row){
    $tmp = array();
    foreach($row as $values){
        $tmp[$values['name']] = $values['value'];
    }
    $newArray[] = $tmp;
}

print_r($newArray);