sphinx9999 Posted November 21, 2007 Share Posted November 21, 2007 Hi, I'm trying to convert an array of text into all of its sequential variations keeping the elements in order, i.e. if the original array is: $original=array('one','two','three','four'); I need to convert it into: $new=array( array('one'), array('one','two'), array('one','two','three'), array('one','two','three','four'), array('two'), array('two','three'), array('two','three','four'), array('three'), array('three','four'), array('four') ); Any ideas how this can be done (for a dynamic number of elements in the original array)? Cheers Quote Link to comment https://forums.phpfreaks.com/topic/78213-solved-array-variations/ Share on other sites More sharing options...
Orio Posted November 21, 2007 Share Posted November 21, 2007 <?php $original=array('one','two','three','four'); echo "<pre>"; print_r(get_sequence($original)); echo "</pre>"; function get_sequence($array) { $size = count($array); $data = array(); for($i=0; $i<$size; $i++) { foreach($array as $key=>$val) $data[] = array_slice($array, 0, $key+1); array_shift($array); } return $data; } ?> Orio. Quote Link to comment https://forums.phpfreaks.com/topic/78213-solved-array-variations/#findComment-395841 Share on other sites More sharing options...
sphinx9999 Posted November 21, 2007 Author Share Posted November 21, 2007 Genius! Good thinking Orio Quote Link to comment https://forums.phpfreaks.com/topic/78213-solved-array-variations/#findComment-395845 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.