note 60994 deleted from ref.array by tularis

From: Date: Mon, 23 Jan 2006 09:37:49 +0000
Subject: note 60994 deleted from ref.array by tularis
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-102268@lists.php.net to get a copy of this message
Note Submitter: skondrat at finishedsolutions dot com Reason: useless ---- Function returns all ways how elements can be ordered in array. Be careful number of ways increases as factorial of number of elements:). function all_orders(&$arr) { $result=array(); if(count($arr)==1) { $result[]=array($arr[0]); } else { for($i=0;$i<count($arr);$i++) { $sub_arr=array(); for($j=0;$j<count($arr);$j++) { if($i!=$j) { $sub_arr[]=$arr[$j]; } } $sub_result=all_orders($sub_arr); for($j=0;$j<count($sub_result);$j++) { $new_order=array($arr[$i]); foreach($sub_result[$j] as $val) { $new_order[]=$val; } $result[]=$new_order; } } } return $result; } $arr=array('a','b','c'); print_r(all_orders($arr)); will produce Array ( [0] => Array ( [0] => a [1] => b [2] => c ) [1] => Array ( [0] => a [1] => c [2] => b ) [2] => Array ( [0] => b [1] => a [2] => c ) [3] => Array ( [0] => b [1] => c [2] => a ) [4] => Array ( [0] => c [1] => a [2] => b ) [5] => Array ( [0] => c [1] => b [2] => a ) )

« previous php.notes (#102268) next »