NYCPHP Meetup

NYPHP.org

[nycphp-talk] Overriding Array's

Joseph Crawford codebowl at gmail.com
Wed May 2 13:52:46 EDT 2007


Guys I have 2 arrays the structures will be similar, for instance here
is a chopped down version

Array
(
    [http_metas] => Array
        (
            [content-type] => text/html; charset=utf-8
            [content-language] => en-us
        )

    [metas] => Array
        (
            [PreventParsing] => true
            [robots] => noindex, nofollow
            [description] => Recruiting and HR news, information and
community -- including articles, discussions, blogs, jobs,
conferences, research, email publications and more.
            [copyright] => 2007
            [keywords] => recruiting, staffing, HR, business
        )
    [page] => Array
        (
            [template] => mainTemplate
            [title] => ERE.net - Recruiting news, information and community
            [ssl] =>
        )
)

The second array we have is the following

Array
(
    [page] => Array
        (
            [title] => Test Page
        )
)

We are looking to have the second array override the first array
values.  I am not sure if i am having a brain fart here or what.  I
have looked into array_walk, custom recursive function and
array_merge_recursive.  I cannot seem to get any of these methods to
work.

Here is the function that I have created

	
	public function override($default, $page)
	{
		foreach($page as $key => $val)
		{
			if(is_array($val)) return $this->override($default[$key], $page[$key]);
			else
			{
				if(isset($page[$key]) && $page[$key] !== "") $default[$key] = $val;
			}
		}
		return $default;
	}

Any assistance would be appreciated.

-- 
Joseph Crawford Jr.
Zend Certified Engineer
Codebowl Solutions, Inc.
http://www.codebowl.com/
Blog: http://www.josephcrawford.com/
1-802-671-2021
codebowl at gmail.com



More information about the talk mailing list