note 115186 added to function.array-merge

From: Date: Tue, 10 Jun 2014 22:49:51 +0000
Subject: note 115186 added to function.array-merge
Groups: php.notes 
Request: Send a blank email to php-notes+get-199644@lists.php.net to get a copy of this message
Sometimes you need to modify an array with another one here is my approach to replace an array's content recursively with delete opiton. Here i used "::delete::" as reserved word to delete items. <?php $person= array( "name" => "Metehan", "surname"=>"Arslan", "age"=>27, "mail"=>"hidden", "favs" => array( "language"=>"php", "planet"=>"mercury", "city"=>"istanbul") ); $newdata = array( "age"=>28, "mail"=>"::delete::", "favs" => array( "language"=>"js", "planet"=>"mercury", "city"=>"shanghai") ); print_r(array_overlay($person,$newdata)); // result: Array ( [name] => Metehan [surname] => Arslan [age] => 28 [favs] => Array ( [language] => js [planet] => mercury [city] => shanghai ) ) function array_overlay($a1,$a2) { foreach($a1 as $k => $v) { if ($a2[$k]=="::delete::"){ unset($a1[$k]); continue; }; 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; } ?> ---- Server IP: 72.52.91.14 Probable Submitter: 141.0.9.77 (proxied: 178.240.214.143) ---- Manual Page -- http://php.net/manual/en/function.array-merge.php Edit -- https://master.php.net/note/edit/115186 Del: integrated -- https://master.php.net/note/delete/115186/integrated Del: useless -- https://master.php.net/note/delete/115186/useless Del: bad code -- https://master.php.net/note/delete/115186/bad+code Del: spam -- https://master.php.net/note/delete/115186/spam Del: non-english -- https://master.php.net/note/delete/115186/non-english Del: in docs -- https://master.php.net/note/delete/115186/in+docs Del: other reasons-- https://master.php.net/note/delete/115186 Reject -- https://master.php.net/note/reject/115186 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#199644) next »