note 90243 added to function.array-merge

From: Date: Sun, 12 Apr 2009 02:28:49 +0000
Subject: note 90243 added to function.array-merge
Groups: php.notes 
Request: Send a blank email to php-notes+get-153103@lists.php.net to get a copy of this message
Untested, so use at your own risk... I came to this page perhaps looking for some code to merge ranges represented by arrays of start/end pairs. Here's what I came up with: <?php function range_merge($a, $b, $s = 's', $e = 'e') { if (!isset($a[$s]) or !isset($a[$e]) or !isset($b[$s]) or !isset($b[$e])) return(false); // ensure that the "start" and "end" values are numerically coherant if ($a[$s] > $a[$e]) $a = array('s'=>$a[$e], 'e'=>$a[$s]); if ($b[$s] > $b[$e]) $b = array('s'=>$b[$e], 'e'=>$b[$s]); if ($a[$s] == $b[$e]) // a starts where b ends return(array('s'=>$b[$s],'e'=>$a[$e])); if ($b[$s] == $a[$e]) // b starts where a ends return(array('s'=>$a[$s],'e'=>$b[$e])); if ($a[$s] >= $b[$s] and $a[$s] < $b[$e]) // a starts in b return(array('s'=>$b[$s],'e'=>max($b[$e], $a[$e]))); if ($b[$s] >= $a[$s] and $b[$s] < $a[$e]) // b starts in a return(array('s'=>$a[$s],'e'=>max($b[$e], $a[$e]))); if ($a[$e] > $b[$s] and $a[$e] <= $b[$e]) // a ends in b return(array('s'=>min($b[$s], $a[$s]),'e'=>$b[$e])); if ($b[$e] > $a[$s] and $b[$e] <= $a[$e]) // b ends in a return(array('s'=>min($b[$s], $a[$s]),'e'=>$a[$e])); return(false); } function range_merge_all($r) { if (!is_array($r)) return(false); $t = array(); // return value while(sizeof($r)) // while we have values left to pop { $g = array_pop($r); if (sizeof($r)) // $g might have popped the last value off foreach($r as $k=>$v) if ($m = range_merge($g, $v)) { $g = $m; unset($r[$k]); // we merged this value away } array_push($t, $g); } return($t); } ?> ---- Server IP: 69.147.83.197 Probable Submitter: 75.72.167.230 ---- Manual Page -- http://www.php.net/manual/en/function.array-merge.php Edit -- https://master.php.net/note/edit/90243 Del: integrated -- https://master.php.net/note/delete/90243/integrated Del: useless -- https://master.php.net/note/delete/90243/useless Del: bad code -- https://master.php.net/note/delete/90243/bad+code Del: spam -- https://master.php.net/note/delete/90243/spam Del: non-english -- https://master.php.net/note/delete/90243/non-english Del: in docs -- https://master.php.net/note/delete/90243/in+docs Del: other reasons-- https://master.php.net/note/delete/90243 Reject -- https://master.php.net/note/reject/90243 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#153103) next »