<?php
// this function merges an array with the $_SESSION
// if you omit second parameter it merges to the root
// if you give one 'path' array it is merged deeply
function arr2sess($arr, $path=array()){
if (!is_array($arr)) return false;
foreach($arr as $k => $v){
if(is_array($arr[$k])) {
$path[] = $k;
arr2sess($arr[$k], $path);
}else{
$ref = &$_SESSION;
if($path){
foreach($path as $val){
if(!$ref[$val]) $ref[$val] = array();
$ref = &$ref[$val];
}
}
$ref[$k] = $v;
}
}
}
session_start();
$x = array(k1=>12, k2=>array(kxx=>'forget me', kyy=>'I was allways here')); // do you have any of this on $_SESSION
$rpl = array(k2=>array(kxx=>'I am a replaced value',kzz=>'I am a new value'));
arr2sess($x, array('deep','deep','in_session')); // you can use this way
arr2sess($x); // or this
arr2sess($rpl); // to merge parts with the $_SESSION
$w = array(120, q=>array(199,100)); // works the same way on numeric keys
arr2sess($w, array('one','two'));
arr2sess($w);
echo '<pre>';
print_r($_SESSION);
?>