note 60994 added to ref.array

From: Date: Mon, 23 Jan 2006 01:45:44 +0000
Subject: note 60994 added to ref.array
Groups: php.notes 
Request: Send a blank email to php-notes+get-102256@lists.php.net to get a copy of this message
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 ) ) ---- Server IP: 195.54.192.44 Probable Submitter: 195.161.106.222 ---- Manual Page -- http://www.php.net/manual/en/ref.array.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+60994 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+60994&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+60994&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+60994&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+60994&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+60994&report=yes&reason=non-english Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+60994&report=yes&reason=already+in+docs Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+60994&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+60994&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#102256) next »