note 89445 added to function.array-merge

From: Date: Sun, 08 Mar 2009 04:33:39 +0000
Subject: note 89445 added to function.array-merge
Groups: php.notes 
Request: Send a blank email to php-notes+get-151621@lists.php.net to get a copy of this message
I needed to merge two arrays while keeping the first arrays format and adding the values, if they exist, from the second array. I was working with several multidimensional arrays and was using one as the primary structure to insert data into a database. This is what I made. I hope it helps. <?php function array_overlay($a1,$a2) { foreach($a1 as $k => $v) { if(!array_key_exists($k,$a2)) continue; if(is_array($v) && is_array($a2[$k])){ $a1[$k] = array_overlay($v,$a2[$k]); }else{ $a1[$k] = $a2[$k]; } } return $a1; } ?> I used it like this: <?php $table = tableDescription(); // created an array of the table structure $table = array_overlay($table,$_POST); // writes the values submitted from the array into the table using array names in the form like <input type="text" name="column[Value]" value="" /> ?> ---- Server IP: 69.147.83.197 Probable Submitter: 71.199.172.114 ---- Manual Page -- http://www.php.net/manual/en/function.array-merge.php Edit -- https://master.php.net/note/edit/89445 Del: integrated -- https://master.php.net/note/delete/89445/integrated Del: useless -- https://master.php.net/note/delete/89445/useless Del: bad code -- https://master.php.net/note/delete/89445/bad+code Del: spam -- https://master.php.net/note/delete/89445/spam Del: non-english -- https://master.php.net/note/delete/89445/non-english Del: in docs -- https://master.php.net/note/delete/89445/in+docs Del: other reasons-- https://master.php.net/note/delete/89445 Reject -- https://master.php.net/note/reject/89445 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#151621) next »