Page 1 of 1

Combining an Array

Posted: Mon Sep 24, 2007 1:01 am
by iknownothing
Hey Guys, I have the following array:

Code: Select all

Array ( 
	[uploadfile] => Array ( 
		[name] => Array ( 
			[0] => icon.png 
			[1] => cp-icon.gif 
			[2] => plans.php 
			[3] => 
		) 
		[type] => Array ( 
			[0] => image/x-png 
			[1] => image/gif 
			[2] => application/octet-stream 
			[3] => 
		) 
		[tmp_name] => Array ( 
			[0] => /tmp/phpvRI6oR 
			[1] => /tmp/phpvYUxmw 
			[2] => /tmp/phpfRz3jb 
			[3] => 
		) 
		[error] => Array ( 
			[0] => 0 
			[1] => 0 
			[2] => 0 
			[3] => 4 
		) 
		[size] => Array ( 
			[0] => 18541 
			[1] => 5347 
			[2] => 8734 
			[3] => 0 
		) 
	) 
)
What PHP function could I use to join the sub-array's together?

For Example, I want:

Code: Select all

Array {
[Filename] => icon.png
[Filetype] => image/x-png
[Tempname] => /tmp/phpvRI6oR
[Error] => 0
[Filesize] => 18541
}
etc.

Notice, in the above, the array is made up of all the [0]'s in the other array.


Thanks.

Posted: Mon Sep 24, 2007 2:46 am
by Hemlata
Hello,

One solution could be as follows..

Code: Select all

$count = count($res['uploadfile']['name']);
$result = array();
for ($i=0; $i<$count; $i++)
{
	$result[$i] = array
	(
		'Filename' => $res['uploadfile']['name'][$i]
		, 'Filetype' => $res['uploadfile']['type'][$i]
		, 'Tempname' => $res['uploadfile']['tmp_name'][$i]
		, 'Error' => $res['uploadfile']['error'][$i]
		, 'Filesize' => $res['uploadfile']['size'][$i]
	);
}
print_r($result);
Hope this might help you

Regards,

Posted: Mon Sep 24, 2007 10:43 am
by feyd
Naming the fields in another way would group them in the desired manner.

Posted: Mon Sep 24, 2007 2:50 pm
by stereofrog
try

Code: Select all

$q = call_user_func_array(
	'array_map',
	array_merge(array(null), $ary['uploadfile'])
);