Merge 2 arrays recursively (sorta)

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
rmccue
Forum Commoner
Posts: 27
Joined: Thu Oct 05, 2006 12:47 am
Location: Gold Coast, Australia

Merge 2 arrays recursively (sorta)

Post by rmccue »

I want to merge 2 arrays recursively, like array_merge_recursive(), but I want the values from the second array to override the second.
For example:

Code: Select all

$blah = array(
	'items'	=> array(
		1 => array(
			'stuff'	=> false
		)
	)
);
//and
$cookie = array(
	'items' => array(
		1 => array(
			'stuff' => true
		)
	)
);
Is there a function for what I want to do, or should I write myself a function?

Also, is there a way of storing an array in a cookie? I know about serialize(), but I'm not sure if the array will fit in a cookie.
User avatar
kaszu
Forum Regular
Posts: 749
Joined: Wed Jul 19, 2006 7:29 am

Post by kaszu »

Maybe this will help

Code: Select all

function _array_merge_recursive($arr1, $arr2)
{
	foreach($arr2 as $k => $v)
	{
		if (is_array($v) and isset($arr1[$k]))
		{
			$arr1[$k] = _array_merge_recursive($arr1[$k], $v);
		}else{
			$arr1[$k] = $v;
		}		
	}
	
	return $arr1;
}
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Re: Merge 2 arrays recursively (sorta)

Post by superdezign »

rmccue wrote:Also, is there a way of storing an array in a cookie? I know about serialize(), but I'm not sure if the array will fit in a cookie.
If you were to do that, then users could alter the data. Why would you want that?
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Merge 2 arrays recursively (sorta)

Post by VladSun »

superdezign wrote:
rmccue wrote:Also, is there a way of storing an array in a cookie? I know about serialize(), but I'm not sure if the array will fit in a cookie.
If you were to do that, then users could alter the data. Why would you want that?
Maybe he uses HMAC ;)
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Re: Merge 2 arrays recursively (sorta)

Post by superdezign »

VladSun wrote:Maybe he uses HMAC ;)
Hehe, of course he does. :P
rmccue
Forum Commoner
Posts: 27
Joined: Thu Oct 05, 2006 12:47 am
Location: Gold Coast, Australia

Re: Merge 2 arrays recursively (sorta)

Post by rmccue »

superdezign wrote: If you were to do that, then users could alter the data. Why would you want that?
Well, I'm writing an aggregator and I want people to be able to star issues. However, I want to store which ones have been starred in a cookie and merge that with the array I generate.

Oh, and no, since it's not critical information, I don't plan to use a HMAC.

Actually, it just occurred to me that that's somewhat unsafe. I guess I'll do it another way, so ignore the first question, but would serialize still be the best for storing an array in a cookie? All values will be boolean and will be verified.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Re: Merge 2 arrays recursively (sorta)

Post by superdezign »

rmccue wrote:Actually, it just occurred to me that that's somewhat unsafe. I guess I'll do it another way, so ignore the first question, but would serialize still be the best for storing an array in a cookie? All values will be boolean and will be verified.
Not necessarily in a cookie, but it is a good way to store an array. It helps PHP keep the data as is. A safer way would be for your users to have accounts and save it in a database entry that is associated with their account.
rmccue
Forum Commoner
Posts: 27
Joined: Thu Oct 05, 2006 12:47 am
Location: Gold Coast, Australia

Re: Merge 2 arrays recursively (sorta)

Post by rmccue »

superdezign wrote:A safer way would be for your users to have accounts and save it in a database entry that is associated with their account.
Yes, but since it's only a flat-file based system, I'm trying to avoid the overhead of a user management system.
I already use serialize; I'm not a total noob :P
Post Reply