merging arrays

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
ibolui
Forum Commoner
Posts: 27
Joined: Thu May 26, 2005 9:41 am

merging arrays

Post by ibolui »

hi guys, need help with some php codes.

Code: Select all

$aa = array('name'=>array('tom'=>'tt', 'dick'=>'dd'));
$bb = array('age'=>array('tom'=>'11','dick'=>'22'));
how can I have the array result below..

Code: Select all

array(

// tom
array('name'=>'tt', 'age'=>'11'),
// dick
array('name'=>'dd', 'age'=>'22'),

)
thanks!!
Peter Kelly
Forum Contributor
Posts: 143
Joined: Fri Jan 14, 2011 5:33 pm
Location: England
Contact:

Re: merging arrays

Post by Peter Kelly »

Code: Select all

$i = "0";
foreach ($aa['name'] as $key => $value){
	$name = $value;
	$age = $bb['age'][$key];
	$array[$i][name] = $name;
	$array[$i][age] = $age;
	$i++;
}
Try that?
ibolui
Forum Commoner
Posts: 27
Joined: Thu May 26, 2005 9:41 am

Re: merging arrays

Post by ibolui »

nope..i wouldnt know what are in the arrays beforehand..
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: merging arrays

Post by VladSun »

:twisted:

Code: Select all

<?php


$aa = array('name'=>array('tom'=>'tt', 'dick'=>'dd'));
$bb = array('age'=>array('tom'=>'11','dick'=>'22'));

$expectedResult = array
(
	'tom'	=> array('name'=>'tt', 'age'=>'11'),
	'dick'	=> array('name'=>'dd', 'age'=>'22'),
);

/*******************************************/

// Weirdan fix
$result = array_merge($aa, $bb);

$names = array_keys(end($result));
$fields = array_keys($result);

array_unshift($result, null);
$result = call_user_func_array('array_map', $result);

array_walk
(
	$result,
	function (&$value, $key, $fields)
	{
		$value = array_combine($fields, $value);
	},
	$fields
);

$result = array_combine($names, $result);

/*******************************************/

assert('print_r($expectedResult, true) === print_r($result, true);');

var_dump($result);
Last edited by VladSun on Thu Jan 20, 2011 8:49 am, edited 2 times in total.
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: merging arrays

Post by Weirdan »

Code: Select all

$aa = array('name'=>array('tom'=>'tt', 'dick'=>'dd'));
$bb = array('age'=>array('tom'=>'11','dick'=>'22'));

$ret = array();

foreach (array_merge($aa, $bb) as $key => $data) {
    foreach ($data as $item => $value) {
         $ret[$item][$key] = $value;
    }
}
$ret = array_values($ret);
var_dump($ret);
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: merging arrays

Post by VladSun »

I am not sure whether the OP meant:

Code: Select all

array(

// tom
array('name'=>'tt', 'age'=>'11'),
// dick
array('name'=>'dd', 'age'=>'22'),

)
=>

Code: Select all

array(
 'tom' =>
array('name'=>'tt', 'age'=>'11'),
 'dick' =>
array('name'=>'dd', 'age'=>'22'),
)
Though it's easy to fix - just comment out $ret = array_values($ret);
Nice solution Weirdan :) It gives me some fixes to my code :) Obviously, I "overcoded" the problem by trying to use the array_* functions.
There are 10 types of people in this world, those who understand binary and those who don't
ibolui
Forum Commoner
Posts: 27
Joined: Thu May 26, 2005 9:41 am

Re: merging arrays

Post by ibolui »

Code: Select all

$aa = array('name'=>array('tom'=>'tt', 'dick'=>'dd'));
$bb = array('age'=>array('tom'=>'11','dick'=>'22'));

$ret = array();

foreach (array_merge($aa, $bb) as $key => $data) {
    foreach ($data as $item => $value) {
         $ret[$item][$key] = $value;
    }
}
$ret = array_values($ret);
var_dump($ret);
thank you!!
Post Reply