JasonDavis
JasonDavis

Reputation: 48963

Manipulate a Multi-Multi-Dimensional PHP Array into a nice Multi-Dimensional Array

I know this is considered a somewhat basic PHP Array question but i'm running on 35 hours no sleep and I really just need to finish this up as quickly as posibble so I can get to sleep...sorry just being honest!

In PHP I have this variable $design_values

If I print_r($design_values); this ARRAY it spits out what is show below. It is "Desing" database records.

In this case there is 2 Design records which make up the first 2 Array keyys the 0 and 1

In the application there can be any number of Designs from 0 up to any number.

Now under the 2 Design Records are 24 more Array keys for each of the 2 Design Arrays.

These 24 Array keys are numbered 0 to 23.

Now under each of the 24 Array keys, is 2 keys. One named name and the other named value.

I need to take the Array $design_values and create a new Array of that array. The new Array should be formatted much better amd easy to work with.

So the name and value keys should make up a key => value. The New array should look more like this....

The reason the current Array is a complete nightmare is because that is the Format I get it in from an existing library which returns this Data from an API call.

If someone can help me to manipulate this Array into the desired Array I will be grateful! I have been messing with it for 2 hours with no luck.

Desired New Array Format :

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
        )
)

Current Array Format :

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] => 
                )

        )

)

Upvotes: 0

Views: 38

Answers (2)

AbraCadaver
AbraCadaver

Reputation: 78994

If you can't change it at the source then here is one way (PHP >= 5.5.0 needed for array_column):

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

Or possibly, and easier:

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

Our use the PHP Implementation of array_column

Upvotes: 3

Jonathan Kuhn
Jonathan Kuhn

Reputation: 15311

You should probably create the array you want when making the first array, but if you don't have control over that and just want to conver then something like this should work:

$newArray = array();

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

print_r($newArray);

Upvotes: 2

Related Questions