How to generate Combinations of fixed size from multiple arrays? - php

I need to find all combinations of items in several arrays with fixed subset size. For example, I have the 3 arrays :
$A = array('A1','A2','A3');
$B = array('B1','B2','B3');
$C = array('C1','C2','C3');
I want to generate combinations of size 2 from the above arrays. Like:
$Combinations = array(
[0] => array('A1', 'B1'),
[1] => array('A1', 'C1'),
[2] => array('A2', 'B1'),
[3] => array('A2', 'C1')
);
This solution is generating all combinations, but does not seem to have size parameter in it.
Looking for help!

$A = array('A1','A2','A3');
$B = array('B1','B2','B3');
$C = array('C1','C2','C3');
$All = array();
foreach ($A as $key1=>$value1){
foreach ($B as $key2=>$value2){
$All[] = array($value1,$value2 );
}
foreach ($C as $key3=>$value3){
$All[] = array($value1,$value3 );
}
}
print_r($All);
Check output here : https://eval.in/574060

Finally, found a solution. With the following script, you can combine any number of arrays with any number of elements per a combination. Please read the comments in the code and try to understand what happens.
<?php
$A = array('A1', 'A2', 'A3');
$B = array('B1', 'B2', 'B3');
$C = array('C1', 'C2', 'C3');
$combinationCount = 5;
$itemsPerCombination = 2;
$array = ['A', 'B', 'C'];
$combinations = array();
for ($x = 0; $x < $combinationCount; $x++) {
//to keep temporary names of arrays which come in a combination
$arrays = array();
for ($y = 0; $y < $itemsPerCombination; $y++) {
$valid = false;
while (!$valid) {
//get a random array, check if it is already in our selection
$arrayElement = $array[rand(0, count($array) - 1)];
if (in_array($arrayElement, $arrays)) {
$valid = false;
continue;
}
$arrays[] = $arrayElement;
$valid = true;
}
}
$found = false;
while (!$found) {
//for each selection in our selected arrays, take a random element and add to the combination.
$combination = array();
foreach ($arrays as $arr) {
$temp=$$arr;
$combination[] = $temp[rand(0, count($temp) - 1)];
}
if (in_array($combination, $combinations)) {
$found = false;
continue;
}
$combinations[] = $combination;
$found = true;
}
}
echo(json_encode($combinations));
?>

Related

How to get the maximum possible sum of values of the edges’ endpoints php

The contents of this question have been removed due to a DMCA Takedown request by Codility Limited.
Here is the Simplets PHP solution for the Above question from the Codility test.
<?php
$A = [2,2,1,2];
$B = [1,3,4,4];
$w = [3,5,2,4,1];
$N = 5;
// $A = [1];
// $B = [3];
// $A = [1,3];
// $B = [2,4];
function solution ($N, $A, $B){
if(count($A) != count($B) || !is_int($N) )
{
return false;
}
$V = [];
$vertextCount = [];
foreach($A as $k=>$val){
if(!isset($vertextCount[$val])){
$vertextCount[$val] = 0;
}
$vertextCount[$val] += 1;
}
foreach($B as $k=>$val){
if(!isset($vertextCount[$val])){
$vertextCount[$val] = 0;
}
$vertextCount[$val] += 1;
}
if($vertextCount < $N)
{
$vertextCount[$N] = 0;
}
$VC = $vertextCount;
$tn = $N;
$wightArr = [];
while(count($VC) > 0){
$maxKey = current(array_keys($VC, max($VC)));
$wightArr[$maxKey] = $tn;
unset($VC[$maxKey]);
$tn--;
}
$sum = 0;
foreach($A as $k=>$val){
$sum += $wightArr[$A[$k]] + $wightArr[$B[$k]];
}
return $sum;
}
echo $sum = solution($N, $A, $B);
NOTE:- Tested against the 3 given Examples in the test, Not sure about all the test cases.

PHP string expansion

I have a string composed by many letters, at some point, one letter from a group can be used and this is represented by letters enclosed in []. I need to expand these letters into its actual strings.
From this:
$str = 'ABCCDF[GH]IJJ[KLM]'
To this:
$sub[0] = 'ABCCDFGIJJK';
$sub[1] = 'ABCCDFHIJJK';
$sub[2] = 'ABCCDFGIJJL';
$sub[3] = 'ABCCDFHIJJL';
$sub[4] = 'ABCCDFGIJJM';
$sub[5] = 'ABCCDFHIJJM';
UPDATE:
Thanks to #Barmar for the very valuable suggestions.
My final solution is:
$str = '[GH]DF[IK]TF[ADF]';
function parseString(string $str) : array
{
$i = 0;
$is_group = false;
$sub = array();
$chars = preg_split('//', $str, -1, PREG_SPLIT_NO_EMPTY);
foreach ($chars as $key => $value)
{
if(ctype_alpha($value))
{
if($is_group){
$sub[$i][] = $value;
} else {
if(!isset($sub[$i][0])){
$sub[$i][0] = $value;
} else {
$sub[$i][0] .= $value;
}
}
} else {
$is_group = !$is_group;
++$i;
}
}
return $sub;
}
The recommended function for combinations is (check the related post):
function array_cartesian_product($arrays)
{
$result = array();
$arrays = array_values($arrays);
$sizeIn = sizeof($arrays);
$size = $sizeIn > 0 ? 1 : 0;
foreach ($arrays as $array)
$size = $size * sizeof($array);
for ($i = 0; $i < $size; $i++) {
$result[$i] = array();
for ($j = 0; $j < $sizeIn; $j++)
array_push($result[$i], current($arrays[$j]));
for ($j = ($sizeIn - 1); $j >= 0; $j--) {
if (next($arrays[$j]))
break;
elseif (isset($arrays[$j]))
reset($arrays[$j]);
}
}
return $result;
}
Check the solution with:
$combinations = array_cartesian_product(parseString($str));
$sub = array_map('implode', $combinations);
var_dump($sub);
Convert your string into a 2-dimensional array. The parts outside brackets become single-element arrays, while each bracketed trings becomes an array of single characters. So your string would become:
$array =
array(array('ABCCDF'),
array('G', 'H', 'I'),
array('IJJ'),
array('K', 'L', 'M'));
Then you just need to compute all the combinations of those arrays; use one of the answers at How to generate in PHP all combinations of items in multiple arrays. Finally, you concatenate each of the resulting arrays with implode to get an array of strings.
$combinations = combinations($array);
$sub = array_map('implode', $combinations);

Split array into a specific number of chunks

I know that array_chunk() allows to split an array into several chunks, but the number of chunks changes according to the number of elements. What I need is to always split the array into a specific number of arrays like 4 arrays for example.
The following code splits the array into 3 chunks, two chunks with 2 elements each and 1 chunk with 1 element. What I would like is to split the array always into 4 chunks, no matter the number of total elements that the array has, but always trying to divide the elements evenly in the chunks like the array_chunck function does. How can I accomplish this? Is there any PHP function for this?
$input_array = array('a', 'b', 'c', 'd', 'e');
print_r(array_chunk($input_array, 2));
print_r(array_chunk($input_array, 2, true));
Thank you.
You can try
$input_array = array(
'a',
'b',
'c',
'd',
'e'
);
print_r(partition($input_array, 4));
Output
Array
(
[0] => Array
(
[0] => a
[1] => b
)
[1] => Array
(
[0] => c
)
[2] => Array
(
[0] => d
)
[3] => Array
(
[0] => e
)
)
Function Used
/**
*
* #param Array $list
* #param int $p
* #return multitype:multitype:
* #link http://www.php.net/manual/en/function.array-chunk.php#75022
*/
function partition(Array $list, $p) {
$listlen = count($list);
$partlen = floor($listlen / $p);
$partrem = $listlen % $p;
$partition = array();
$mark = 0;
for($px = 0; $px < $p; $px ++) {
$incr = ($px < $partrem) ? $partlen + 1 : $partlen;
$partition[$px] = array_slice($list, $mark, $incr);
$mark += $incr;
}
return $partition;
}
So many complex answers here. I'll post my simple solution :)
function splitMyArray(array $input_array, int $size, $preserve_keys = null): array
{
$nr = (int)ceil(count($input_array) / $size);
if ($nr > 0) {
return array_chunk($input_array, $nr, $preserve_keys);
}
return $input_array;
}
Usage:
$newArray = splitMyArray($my_array, 3);
More details here: https://zerowp.com/split-php-array-in-x-equal-number-of-elements/
Divide the size of the array with the number of chunks you want and supply that as the size of each chunk.
function array_chunks_fixed($input_array, $chunks=3 /*Default chunk size 3*/) {
if (sizeof($input_array) > 0) {
return array_chunk($input_array, intval(ceil(sizeof($input_array) / $chunks)));
}
return array();
}
And call it like this:
array_chunks_fixed($myarray, 2); //override the default number of '3'
This what i write and work well
print_r(array_divide($input,3));
function array_divide($array, $segmentCount) {
$dataCount = count($array);
if ($dataCount == 0) return false;
$segmentLimit = 1;
//if($segmentCount > $segmentLimit)
// $segmentLimit = $segmentCount;
$outputArray = array();
$i = 0;
while($dataCount >= $segmentLimit) {
if( $segmentCount == $i)
$i = 0;
if(!array_key_exists($i, $outputArray))
$outputArray[$i] = array();
$outputArray[$i][] = array_splice($array,0,$segmentLimit)[0] ;
$dataCount = count($array);
$i++;
}
if($dataCount > 0) $outputArray[] = $array;
return $outputArray;
}
Another implementation similar to #Baba's partition() function.
// http://php.net/manual/en/function.array-slice.php#94138
// split the given array into n number of pieces
function array_split($array, $pieces=2)
{
if ($pieces < 2)
return array($array);
$newCount = ceil(count($array)/$pieces);
$a = array_slice($array, 0, $newCount);
$b = array_split(array_slice($array, $newCount), $pieces-1);
return array_merge(array($a),$b);
}
// Examples:
$a = array(1,2,3,4,5,6,7,8,9,10);
array_split($a, 2); // array(array(1,2,3,4,5), array(6,7,8,9,10))
array_split($a, 3); // array(array(1,2,3,4), array(5,6,7), array(8,9,10))
array_split($a, 4); // array(array(1,2,3), array(4,5,6), array(7,8), array(9,10))
This should work:
function getChunks(array $array, $chunks)
{
if (count($array) < $chunks)
{
return array_chunk($array, 1);
}
$new_array = array();
for ($i = 0, $n = floor(count($array) / $chunks); $i < $chunks; ++$i)
{
$slice = $i == $chunks - 1 ? array_slice($array, $i * $n) : array_slice($array, $i * $n, $n);
$new_array[] = $slice;
}
return $new_array;
}
$input_array = array('a', 'b', 'c', 'd', 'e');
echo '<pre>' . print_r(getChunks($input_array, 4), TRUE) . '</pre>';
If someone is looking for a solution to divide the super array into smaller number of separate arrays.
$count = count($appArray); //$appArray contains all elements
$repoCount = 3; //No. of parts to divide
$n = floor($count/$repoCount);
$rem = $count % $repoCount;
$j=1;
while($j <= $repoCount){
${"arr_" . $j} = array();
$j++;
}
$j=0;
$k=1;
for($i=0; $i < $count; $i++){
if($j < $n){
array_push(${"arr_" . $k}, $appArray[$i]);
$j++;
}
else if($k < $repoCount){
$j=0;
$k++;
--$i;
}
if($i >= ($count-$rem)){
$k=1;
for($j=0; $j < $rem; $j++, $i++, $k++){
array_push(${"arr_" . $k},$appArray[$i]);
}
break;
}
}

Finding 4 highest values from an array

Instead of just 1, how can I pick the 4 highest values from an array using max()?
You could use an SplMaxHeap
function maxN(array $numbers, $n)
{
$maxHeap = new SplMaxHeap;
foreach($numbers as $number) {
$maxHeap->insert($number);
}
return iterator_to_array(
new LimitIterator($maxHeap, 0, $n)
);
}
Usage (demo):
print_r( maxN( array(7,54,2,4,26,7,82,4,34), 4 ) );
You could try this:
$a = array(3,5,6,1,23,6,78,99);
asort($a);
var_dump(array_slice($a, -4));
HTH.
This will do it in Θ(n) time:
$a = $b = $c = $d = null;
foreach($array as $v) {
if(!isset($a) || $v > $a) {
$d = $c;
$c = $b;
$b = $a;
$a = $v;
}elseif(!isset($b) || $v > $b) {
$d = $c;
$c = $b;
$b = $v;
}elseif(!isset($c) || $v > $c) {
$d = $c;
$c = $v;
}elseif(!isset($d) || $v > $d) {
$d = $v;
}
}
$result = array($a, $b, $c, $d);
function maxs($ar, $count=4)
{
$res = array();
foreach ($ar as $v)
{
for ($i = 0;$i < $count;$i++)
{
if ($i >= count($res) || $v > $res[$i])
{
do
{
$tmp = $res[$i];
$res[$i] = $v;
$v = $tmp;
$i++;
}
while ($i < $count);
break;
}
}
}
return $res;
}
A simple method using php predefined functions.
<?php
$arr = array(6, 8, 3, 2, 7, 9);
rsort($arr);
$first = array_shift($arr);
$second = array_shift($arr);
$third = array_shift($arr);
echo $first; // print 9
echo $second; // print 8
echo $third; // print 7
?>
While storing itself you can maintain another array as soon as the new item is inserted check with the max value in the inner array if the item being inserted is greater insert this item. During the item pop do viceversa. From the inner maintained array you can get as many max numbers as possible.

How to convert every two consecutive values of an array into a key/value pair?

I have an array like the following:
array('category_name:', 'c1', 'types:', 't1')
I want the alternate values of an array to be the values of an array:
array('category_name:' => 'c1', 'types:' => 't1')
You could try: (untested)
$data = Array("category_name:","c1","types:","t1"); //your data goes here
for($i=0, $output = Array(), $max=sizeof($data); $i<$max; $i+=2) {
$key = $data[$i];
$value = $data[$i+1];
$output[$key] = $value;
}
Alternatively: (untested)
$output = Array();
foreach($data as $key => $value):
if($key % 2 > 0) { //every second item
$index = $data[$key-1];
$output[$index] = $value;
}
endforeach;
function fold($a) {
return $a
? array(array_shift($a) => array_shift($a))
+ fold($a)
: array();
}
print_r(fold(range('a','p' )));
~)
upd: a real-life version
function fold2($a) {
$r = array();
for($n = 1; $n < count($a); $n += 2)
$r[$a[$n - 1]] = $a[$n];
return $r;
}
Here is another yet complex solution:
$keys = array_intersect_key($arr, array_flip(range(0, count($arr)-1, 2)));
$values = array_intersect_key($arr, array_flip(range(1, count($arr)-1, 2)));
$arr = array_combine($keys, $values);
$array = your array;
$newArray['category_name:'] = $array[1];
$newArray['types:'] = $array[3];

Categories