note 100217 added to function.array-merge-recursive

From: Date: Fri, 01 Oct 2010 20:17:51 +0000
Subject: note 100217 added to function.array-merge-recursive
Groups: php.notes 
Request: Send a blank email to php-notes+get-172880@lists.php.net to get a copy of this message
<?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); ?> ---- Server IP: 190.2.55.64 Probable Submitter: 190.225.100.143 ---- Manual Page -- http://www.php.net/manual/en/function.array-merge-recursive.php Edit -- https://master.php.net/note/edit/100217 Del: integrated -- https://master.php.net/note/delete/100217/integrated Del: useless -- https://master.php.net/note/delete/100217/useless Del: bad code -- https://master.php.net/note/delete/100217/bad+code Del: spam -- https://master.php.net/note/delete/100217/spam Del: non-english -- https://master.php.net/note/delete/100217/non-english Del: in docs -- https://master.php.net/note/delete/100217/in+docs Del: other reasons-- https://master.php.net/note/delete/100217 Reject -- https://master.php.net/note/reject/100217 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#172880) next »