Combining an 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
iknownothing
Forum Contributor
Posts: 337
Joined: Sun Dec 17, 2006 11:53 pm
Location: Sunshine Coast, Australia

Combining an Array

Post 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.
Hemlata
Forum Commoner
Posts: 35
Joined: Mon Sep 10, 2007 5:40 am
Location: India
Contact:

Post 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,
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Naming the fields in another way would group them in the desired manner.
User avatar
stereofrog
Forum Contributor
Posts: 386
Joined: Mon Dec 04, 2006 6:10 am

Post by stereofrog »

try

Code: Select all

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