Page 1 of 1
Merge 2 arrays recursively (sorta)
Posted: Sun Sep 16, 2007 3:03 am
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.
Posted: Sun Sep 16, 2007 5:35 am
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;
}
Re: Merge 2 arrays recursively (sorta)
Posted: Sun Sep 16, 2007 8:02 am
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?
Re: Merge 2 arrays recursively (sorta)
Posted: Sun Sep 16, 2007 8:04 am
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

Re: Merge 2 arrays recursively (sorta)
Posted: Sun Sep 16, 2007 8:14 am
by superdezign
VladSun wrote:Maybe he uses HMAC

Hehe, of course he does.

Re: Merge 2 arrays recursively (sorta)
Posted: Mon Sep 17, 2007 1:52 am
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.
Re: Merge 2 arrays recursively (sorta)
Posted: Mon Sep 17, 2007 6:38 am
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.
Re: Merge 2 arrays recursively (sorta)
Posted: Mon Sep 17, 2007 7:06 am
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
