All pastes #467975 Raw Edit

k4st

public text v1 · immutable
·published 2007-05-02 20:41 UTC
rendered paste body
<?php

// goal, overwrite things with assoc keys, append where numeric keys

/*
$a = array(
				'testa' => array(
								'a' => 'a1',
								'b' => 'b1',
								'c' => array(
											'd' => 'd1',
											'e' => 'e1',
											),
								),
				'testb' => array(
								'a' => 'a1',
								'b' => 'b1',
								'c' => array(
											0 => 'd1',
											1 => 'e1',
											2 => 'f1',
											),
								),
				);


$b = array(
				'testa' => array(
								'a' => 'test',
								'c' => array(
											'd' => 'test',
											),
								),
				'testb' => array(
								'c' => array(
											0 => 'g1',
											1 => 'h1',
											2 => 'i1',
											),
								),
				'testc' => 'hi',
				);

*/

//echo var_export(unserialize('a:2:{s:7:"default";a:5:{s:10:"http_metas";a:2:{s:12:"content-type";s:24:"text/html; charset=utf-8";s:16:"content-language";s:5:"en-us";}s:5:"metas";a:5:{s:14:"PreventParsing";s:4:"true";s:6:"robots";s:17:"noindex, nofollow";s:11:"description";s:150:"Recruiting and HR news, information and community -- including articles, discussions, blogs, jobs, conferences, research, email publications and more.";s:9:"copyright";i:2007;s:8:"keywords";s:34:"recruiting, staffing, HR, business";}s:5:"links";a:3:{s:11:"stylesheets";a:3:{i:0;a:2:{s:10:"stylesheet";s:9:"reset.css";s:5:"media";s:3:"all";}i:1;a:2:{s:10:"stylesheet";s:10:"common.css";s:5:"media";s:3:"all";}i:2;a:2:{s:10:"stylesheet";s:10:"global.css";s:5:"media";s:18:"screen, projection";}}s:13:"shortcut icon";s:26:"/images/design/favicon.ico";s:11:"javascripts";a:4:{i:0;s:9:"jquery.js";i:1;s:12:"interface.js";i:2;s:17:"jquery.plugins.js";i:3;s:9:"global.js";}}s:4:"page";a:3:{s:8:"template";s:12:"mainTemplate";s:5:"title";s:52:"ERE.net - Recruiting news, information and community";s:3:"ssl";b:0;}s:3:"ads";a:3:{s:6:"top_ad";b:0;s:8:"right_ad";b:1;s:9:"bottom_ad";b:0;}}s:15:"auth/login/test";a:2:{s:4:"page";a:1:{s:5:"title";s:9:"Test Page";}s:5:"links";a:1:{s:11:"stylesheets";a:1:{i:0;a:2:{s:10:"stylesheet";s:8:"test.css";s:5:"media";s:4:"test";}}}}}'));
//exit;

$arrays = array (
  'default' => 
  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',
    ),
    'links' => 
    array (
      'stylesheets' => 
      array (
        0 => 
        array (
          'stylesheet' => 'reset.css',
          'media' => 'all',
        ),
        1 => 
        array (
          'stylesheet' => 'common.css',
          'media' => 'all',
        ),
        2 => 
        array (
          'stylesheet' => 'global.css',
          'media' => 'screen, projection',
        ),
      ),
      'shortcut icon' => '/images/design/favicon.ico',
      'javascripts' => 
      array (
        0 => 'jquery.js',
        1 => 'interface.js',
        2 => 'jquery.plugins.js',
        3 => 'global.js',
      ),
    ),
    'page' => 
    array (
      'template' => 'mainTemplate',
      'title' => 'ERE.net - Recruiting news, information and community',
      'ssl' => false,
    ),
    'ads' => 
    array (
      'top_ad' => false,
      'right_ad' => true,
      'bottom_ad' => false,
    ),
  ),
  'auth/login/test' => 
  array (
    'page' => 
    array (
      'title' => 'Test Page',
    ),
    'links' => 
    array (
      'stylesheets' => 
      array (
        0 => 
        array (
          'stylesheet' => 'test.css',
          'media' => 'test',
        ),
      ),
	'javascripts' => 
      array (
        0 => 'test.js',
		1 => 'test2.js',
      ),
    ),
  ),
);

$a = $arrays['default'];
$b = $arrays['auth/login/test'];

// parse through an array, $a is the original array and
// $b is the new array to merge into it
function parse_array($a, $b)
{
	// an array that will hold the new, properly
	// merged version of the two arrays
	$new = array();
	
	// if we've encountered numbered keys, this
	// will be true
	$numbers = FALSE;
	
	// if $a is bigger than $b, lots of code repetition
	// beyond this point.. ugh.
	if(count($a) >= count($b))
	{
		// override the arrays if necessary
		foreach($a as $key => $val)
		{
			// if we've found a number, stop the loop
			if(is_numeric($key)) // dunno why ctype_digit didn't work...
			{
				$numbers = TRUE;
				break;
			}
			
			// otherwise, override the key with a $b value
			// if it exists.
			else
				$new[$key] = isset($b[$key]) ? $b[$key] : $a[$key];
		}
	}
	
	// otherwise..
	else
	{
		
		// override the arrays if necessary
		foreach($b as $key => $val)
		{
			if(is_numeric($key))
			{
				$numbers = TRUE;
				break;
			}
			
			// otherwise, override the key with an $a value
			// if it exists.
			else
				$new[$key] = isset($a[$key]) ? $a[$key] : $b[$key];
		}
	}
	
	// merge the arrays if they are numbered keys
	
	if($numbers)
		$new = array_merge($a, $b);
	
	// loop through the new array and check for sub-arrays
	foreach($new as $key => $val)
	{
		// recursively go through
		if(is_array($new[$key]) && !$numbers)
			$new[$key] = parse_array((isset($a[$key]) ? $a[$key] : array()), (isset($b[$key]) ? $b[$key] : array()));
	}
	
	
		
	// return the new, merged array
	return $new;
}
	

$new = parse_array($a, $b);

print_r($new);

?>