note 93696 added to function.array-combine
| From: | xavier at api-meal dot eu | Date: | Wed, 23 Sep 2009 10:37:58 +0000 |
| Subject: | note 93696 added to function.array-combine | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-160972@lists.php.net to get a copy of this message | ||
<?php
/**
* Return alternatives defined by values of each parameters.
*
* Exemple :
*
* array_alternatives(array('foo','bar'), array('baz',
'qux'));
* array(
* array('foo', 'baz'),
* array('bar', 'baz'),
* array('foo', 'qux'),
* array('bar', 'qux'),
* );
*
* array_alternatives(array('a'), array('simple-minded'),
array('solution'));
* array(
* array('a', 'simple-minded', 'solution')
* );
*
* array_alternatives(array('a'), array('red', 'blue'),
array('car'));
* array(
* array('a', 'red', 'car'),
* array('a', 'blue', 'car'),
* );
*
* @param array $first_element
* @param array $second_element
* @return array
* @author Xavier Barbosa
*/
function array_alternatives(array $first_element, array $second_element)
{
$lists = func_get_args();
$total_lists = func_num_args();
for($i=0; $i<$total_lists; $i++)
{
$list =& $lists[$i];
if (is_array($list) === FALSE)
throw new Exception("Parameter $i is not an array.");
if (count($list) === 0)
throw new Exception("Parameter $i has no element.");
unset($list);
}
// Initialize our alternatives
$alternatives = array();
foreach($lists[0] as &$value)
{
array_push($alternatives, array($value));
unset($value);
}
unset($lists[0]);
// Process alternatives
for($i=1; $i<$total_lists; $i++)
{
$list =& $lists[$i];
$new_alternatives = array();
foreach($list as &$value)
{
foreach($alternatives as $_)
{
array_push($_, $value);
array_push($new_alternatives, $_);
}
}
// Rotate references, it's cheaper than copy array like
$alternatives =
$new_alternatives;
$alternatives =& $new_alternatives;
unset($new_alternatives, $list, $lists[$i]);
}
return $alternatives;
}
?>
----
Server IP: 195.221.21.36
Probable Submitter: 193.93.127.141
----
Manual Page -- http://www.php.net/manual/en/function.array-combine.php
Edit -- https://master.php.net/note/edit/93696
Del: integrated -- https://master.php.net/note/delete/93696/integrated
Del: useless -- https://master.php.net/note/delete/93696/useless
Del: bad code -- https://master.php.net/note/delete/93696/bad+code
Del: spam -- https://master.php.net/note/delete/93696/spam
Del: non-english -- https://master.php.net/note/delete/93696/non-english
Del: in docs -- https://master.php.net/note/delete/93696/in+docs
Del: other reasons-- https://master.php.net/note/delete/93696
Reject -- https://master.php.net/note/reject/93696
Search -- https://master.php.net/manage/user-notes.php