Switch dimension of array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
samtherobot
Forum Newbie
Posts: 1
Joined: Wed Apr 23, 2008 3:03 pm
Location: London, Ontario Canada

Switch dimension of array

Post by samtherobot »

It's hard to give a subject that describes what I'm looking to do so I'll demonstrate.

In this example I want to take the "upload" element from the $_FILES array of a form posting. By default the array returns as such:

Code: Select all

Array
(
    [upload] => Array
        (
            [name] => Array
                (
                    [0] => jeremy_yahoo.gif
                    [1] => n559957488_4483.jpg
                    [2] => IJ4-WP-84-1280.jpg
                    [3] => jeremy_yahoo.gif
                )
        
            [type] => Array
                (
                    [0] => image/gif
                    [1] => image/jpeg
                    [2] => image/jpeg
                    [3] => image/gif
                )
        
            [tmp_name] => Array
                (
                    [0] => /tmp/phpIOaB5C
                    [1] => /tmp/php0ny3P1
                    [2] => /tmp/php0qIMAq
                    [3] => /tmp/phpC5gzpP
                )
        
            [error] => Array
                (
                    [0] => 0
                    [1] => 0
                    [2] => 0
                    [3] => 0
                )
        
            [size] => Array
                (
                    [0] => 10539
                    [1] => 10999
                    [2] => 244962
                    [3] => 10539
                )
        )
)
However I want it so that each element in the array represents a file with various attributes more like:

Code: Select all

 
Array
(
    [0] => Array
        (
            [name] => jeremy_yahoo.gif
            [type] => image/gif
            [tmp_name] => /tmp/phpIOaB5C
            [error] => 0
            [size] => 10539
        )
    [1] => Array
        (
            [name] => n559957488_4483.jpg
            [type] => image/jpeg
            [tmp_name] => /tmp/php0ny3P1
            [error] => 0
            [size] => 10999
        )
    [2] => Array
        (
            [name] => IJ4-WP-84-1280.jpg
            [type] => image/jpeg
            [tmp_name] => /tmp/php0qIMAq
            [error] => 0
            [size] => 244962
        )
    [3] => Array
        (
            [name] => jeremy_yahoo.gif
            [type] => image/gif
            [tmp_name] => /tmp/phpC5gzpP
            [error] => 0
            [size] => 10539
        )
)
 
Basically I'm wondering if there is a specific PHP function or quick way I don't know about. This is how I'm doing it currently:

Code: Select all

 
// create single array of file information to loop through
 
$upload_file_details = array_merge($_POST, $_FILES['upload']);
 
for ($i = 0; $i < count($upload_file_details['filetype']); $i++) {
     $upload_file[$i]['filetype'] = $upload_file_details['filetype'][$i];
     $upload_file[$i]['name'] = $upload_file_details['name'][$i];
     $upload_file[$i]['type'] = $upload_file_details['type'][$i];
     $upload_file[$i]['tmp_name'] = $upload_file_details['tmp_name'][$i];
     $upload_file[$i]['error'] = $upload_file_details['tmp_name'][$i];
     $upload_file[$i]['size'] = $upload_file_details['size'][$i];
}
 
thanks!
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Switch dimension of array

Post by VladSun »

:) Not sure that it will be faster or better than your solution:

Code: Select all

 
function rotate90($item, $key, &$params)
{
    static $ix = 0;
    
    $params['result'][$ix % $params['count']]  [$params['keys'][floor($ix / $params['count'])]] = $item; 
    $ix ++;
}
 
$result = array();
 
$params = array(
    'keys'      => array_keys($upload), 
    'count'     => count($upload['name']), 
    'result'    => &$result,
    );
 
array_walk_recursive($upload, 'rotate90', $params);
print_r($params['result']);
 
There are 10 types of people in this world, those who understand binary and those who don't
Post Reply