Jump to content

[SOLVED] Array variations


sphinx9999

Recommended Posts

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

Link to comment
Share on other sites

<?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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.