Flatten Array: Keep index, value equal to position in array - php

I've been having a little trouble trying to flatten arrays in a specific way.
Here is a print_r view of the array I want to flatten:
Array
(
[1] => Array
(
[8] => 1
[9] => 2
[10] => Array
(
[15] => Array
(
[22] => 1
)
[21] => 2
)
[11] => Array
(
[16] => Array
(
[23] => 1
)
)
)
[2] => Array
(
[12] => 1
)
[3] => Array
(
[13] => 1
)
[4] => Array
(
[14] => 1
)
[5] => 5
[6] => 6
[7] => 7
)
What I'm attempting to create is an array which keeps the above indexes, but the value is equal to it's position in the array, much like the original index (starting from zero).
Here is the desired result:
Array
(
[1] => 1
[2] => 2
[3] => 3
[4] => 4
[5] => 5
[6] => 6
[7] => 7
[8] => 1
[9] => 2
[10] => 3
[11] => 4
[12] => 1
[13] => 1
[14] => 1
[15] => 1
[16] => 1
[21] => 2
[22] => 2
[23] => 1
)
Knowingly, 17 to 20 are missing.
My function is as follows:
function array_flatten ($array) {
$result = array ();
$count = 1;
while ($index = current($array)) {
$result[key($array)] = $count;
if (is_array($index)) {
$result = array_merge(array_flatten($index), $result);
}
next($array);
$count++;
}
return $result;
}
The line $result = array_merge(array_flatten($index), $result); appears to be the problems. It returns:
Array
(
[1] => 1
[2] => 2
[3] => 3
[4] => 4
[5] => 5
[6] => 6
[7] => 7
)
However, if I run var_dump(array_flatten($index)); on the same line, it returns all the arrays I wish to merge to the $result variable.
array
22 => int 1
array
15 => int 1
21 => int 2
array
23 => int 1
array
16 => int 1
array
8 => int 1
9 => int 2
10 => int 3
11 => int 4
array
12 => int 1
array
13 => int 1
array
14 => int 1
It seems that that array_merge doesn't actually merge these arrays.
Is there something I am doing wrong? Any words of guidance are very much appreciated. Thank you.
Update
Solved!
The function which does the required is as follows:
function array_flatten($array, &$result = array()) {
$count = 1;
foreach($array as $index => $value) {
$result[$index] = $count;
if(is_array($value)) {
array_flatten($value, $result);
}
$count++;
}
return $result;
}

function flatten_array($array, &$result) {
foreach($array as $key => $value) {
if(is_array($value)) {
flatten_array($value, $result);
} else {
$result[$key] = $value;
}
}
}
To use this, check the sample code below:
$flattened = array();
$test = array(
1 => 1
, 3 => 2
, array(2 => 4, 4 => 6)
, 5 => 3
, array(7 => 9, 8 => 7, 9 => 5)
);
flatten_array($test, $flattened);
// Now $flattened contains the flattened array

After you have clarified your question, I was a bit surprised that you accepted an answer that does not return the data you expected. (I've now seen you added your solution to your question.)
What I did: I took the function from #Arjan as a base, and run it on the questions input data and compared it with the questions expected data. Then I worked on it a bit. This is what I came up with (as a result):
# COMP EXPECTED . ACTUAL
#00: == Array . Array
#01: == ( . (
#02: == [1] => 1 . [1] => 1
#03: == [2] => 2 . [2] => 2
#04: == [3] => 3 . [3] => 3
#05: == [4] => 4 . [4] => 4
#06: == [5] => 5 . [5] => 5
#07: == [6] => 6 . [6] => 6
#08: == [7] => 7 . [7] => 7
#09: == [8] => 1 . [8] => 1
#10: == [9] => 2 . [9] => 2
#11: == [10] => 3 . [10] => 3
#12: == [11] => 4 . [11] => 4
#13: == [12] => 1 . [12] => 1
#14: == [13] => 1 . [13] => 1
#15: == [14] => 1 . [14] => 1
#16: == [15] => 1 . [15] => 1
#17: == [16] => 1 . [16] => 1
#18: == [21] => 2 . [21] => 2
#19: != [22] => 2 . [22] => 1
#20: == [23] => 1 . [23] => 1
#21: == ) . )
#22: == .
It looks like that your expected data has a mistake for position 22.
This is the modified function (Demo):
function flatten_array($array, &$result = null) {
$r = null === $result;
$i = 0;
foreach($array as $key => $value) {
$i++;
if(is_array($value)) {
$result[$key] = $i;
flatten_array($value, $result);
} else {
$result[$key] = $value;
}
}
if ($r) {
ksort($result);
return $result;
}
}
$actual = flatten_array($input);

Related

Count specific variables in array

I have two different arrays:
$dep = Array ( [0] => 1 [1] => 1 [2] => 1 [3] => 2 [4] => 3 [5] => 3 )
and
$q1_a = Array ( [0] => 4 [1] => 4 [2] => 4 [3] => 2 [4] => 4 [5] => 2 )
I put it in one array:
$arr = array($dep, $q1_a);
And then I got:
Array (
[0] => Array ( [0] => 1 [1] => 1 [2] => 1 [3] => 2 [4] => 3 [5] => 3 )
[1] => Array ( [0] => 4 [1] => 4 [2] => 4 [3] => 2 [4] => 4 [5] => 2 ) )
How can I summarize variables of second array where it matches specific number in first. So I need to find sum of numbers where first array has 1,2 and 3. And to get 3 different numbers.
so first number will be:
[0] => 1 [1] => 1 [2] => 1
[0] => 4 [1] => 4 [2] => 4
4+4+4=12
second:
[3] => 2
[3] => 2
2
third:
[4] => 3 [5] => 3
[4] => 4 [5] => 2
4+2=6
How can I do that?
Try this, live demo.
<?php
$dep = [1,1,1,2,3,3];
$q1_a = [4,4,4,2,4,2];
$flag = current($dep);
$result = [0];
foreach($dep as $k => $v){
if($flag == $v) {
$val = end($result);
$result[key($result)]= $val + $q1_a[$k];
}
else
$result[] = $q1_a[$k];
$flag = $v;
}
print_r($result);
$cur_index = null;
$result = [];
$j = 0;
for ( $i = 0; $i < length($dep); $i++) {
if ($cur_index == null){
$result[$j] = $q1_a[$i];
$cur_index = $dep[i];
}
else if ($cur_index == $dep[$i]) {
$result[$j] += $q1_a[$i]
}
else {
$cur_index = $dep[i];
$result[++$j] += $q1_a[$i];
}
}
Hope this work and this syntax may not correct.

Find all possible permutations without repeating the value

Let's say I have this set of arrays as input:
[
0 => [1,2,4,5],
1 => [2,3,4],
2 => [1,3],
]
I would like to find all permutations possible selecting one value from each array. That value would be unique in the final result, so it won't repeat. For example, I can't have 1 twice in the result.
The count of arrays on input is the same as count of arrays on output.
Examples of combinations wanted (key=>value):
[0 => 1,1 => 2,2 => 3]
[0 => 2,1 => 3,2 => 1]
[0 => 5,1 => 2,2 => 1]
[0 => 1,1 => 3,2 => null]
Wrong results
[0 => 1,1 => 2,2 => 1]
or
[0 => 2,1 => 2,2 => 3]
I would like to get set of all possible permutations using PHP. How can I do that?
I have attached real data set http://pastebin.com/U6Hyawm4 However, I have no idea how many permutations there may be.
Here's a non-recursive version that's also optimized
/**
* Generates all the possible unique N-tuples from an array of N arrays of integers
*
* #param array $input
* #return array
*/
function generateCombinations(array &$input) {
// since the example results included [1, 3, null] I have assumed that
// null is a possible value of each set.
$sets = [];
foreach($input as $set) {
if(!in_array(null, $set)) {
$set[] = null;
}
$sets[] = $set;
}
// by working on the iterators of each array this loop
// linearizes the entire set of possible combinations
// and iterates it (skipping as many as it can).
$output = [];
$setCount = count($sets);
while(current($sets[0]) !== false) {
$testCombo = [];
for($setIdx = 0; $setIdx < $setCount; $setIdx++) {
if(!in_array(current($sets[$setIdx]), $testCombo)) {
$testCombo[] = current($sets[$setIdx]);
}
else {
// when a combination is thrown out as duplicate
// iterate to skip any other combo's that would also
// contain that duplicate
iterateSets($sets, $setIdx);
break;
}
}
// if there were no duplicates add it to the output and iterate
if(count($testCombo) == $setCount) {
$output[] = $testCombo;
iterateSets($sets, $setCount - 1);
}
}
return $output;
}
/**
* Iterates to the next potentially valid combination. I think of
* this like doing long-hand addition. Add 1 and carry is akin to
* next and reset.
*
* #param array $sets
* #param $index
*/
function iterateSets(array &$sets, $index) {
// reset iterators of all sets past the current one to skip
// combos that cannot be valid
for($i = $index + 1, $ic = count($sets); $i < $ic; $i++) {
reset($sets[$i]);
}
// always move one on current set
next($sets[$index]);
while($index > 0 && current($sets[$index]) === false) {
// wrap if current set is at the end
reset($sets[$index]);
$index--;
// move one for the preceding set
next($sets[$index]);
// then repeat
}
}
The resulting array is:
[
[1,2,3]
[1,2,null]
[1,3,null]
[1,4,3]
[1,4,null]
[1,null,3]
[2,3,1]
[2,3,null]
[2,4,1]
[2,4,3]
[2,4,null]
[2,null,1]
[2,null,3]
[4,2,1]
[4,2,3]
[4,2,null]
[4,3,1]
[4,3,null]
[4,null,1]
[4,null,3]
[5,2,1]
[5,2,3]
[5,2,null]
[5,3,1]
[5,3,null]
[5,4,1]
[5,4,3]
[5,4,null]
[5,null,1]
[5,null,3]
[null,2,1]
[null,2,3]
[null,3,1]
[null,4,1]
[null,4,3]
]
Here's an inefficient version:
$input = array(
[1,2,4,5],
[2,3,4],
[1,3]
);
function appendUnique($subs, $i) {
global $input;
if ($i == count($input)) {
return $subs;
}
$output = array();
foreach ($subs as $sub) {
foreach ($input[$i] as $v) {
$new_sub = array_values($sub);
if (in_array($v, $sub)) {
$new_sub[] = null;
} else {
$new_sub[] = $v;
}
$output[] = $new_sub;
}
}
return appendUnique($output, $i+1);
}
$output = appendUnique([[]], 0);
$output_json = array();
foreach ($output as $row) {
$output_json[] = json_encode($row);
}
$output_json = array_unique($output_json);
$deduped = array();
foreach ($output_json as $json) {
$deduped[] = json_decode($json);
}
print_r($deduped);
outputs:
Array
(
[0] => Array
(
[0] => 1
[1] => 2
[2] =>
)
[1] => Array
(
[0] => 1
[1] => 2
[2] => 3
)
[2] => Array
(
[0] => 1
[1] => 3
[2] =>
)
[3] => Array
(
[0] => 1
[1] => 4
[2] =>
)
[4] => Array
(
[0] => 1
[1] => 4
[2] => 3
)
[5] => Array
(
[0] => 2
[1] =>
[2] => 1
)
[6] => Array
(
[0] => 2
[1] =>
[2] => 3
)
[7] => Array
(
[0] => 2
[1] => 3
[2] => 1
)
[8] => Array
(
[0] => 2
[1] => 3
[2] =>
)
[9] => Array
(
[0] => 2
[1] => 4
[2] => 1
)
[10] => Array
(
[0] => 2
[1] => 4
[2] => 3
)
[11] => Array
(
[0] => 4
[1] => 2
[2] => 1
)
[12] => Array
(
[0] => 4
[1] => 2
[2] => 3
)
[13] => Array
(
[0] => 4
[1] => 3
[2] => 1
)
[14] => Array
(
[0] => 4
[1] => 3
[2] =>
)
[15] => Array
(
[0] => 4
[1] =>
[2] => 1
)
[16] => Array
(
[0] => 4
[1] =>
[2] => 3
)
[17] => Array
(
[0] => 5
[1] => 2
[2] => 1
)
[18] => Array
(
[0] => 5
[1] => 2
[2] => 3
)
[19] => Array
(
[0] => 5
[1] => 3
[2] => 1
)
[20] => Array
(
[0] => 5
[1] => 3
[2] =>
)
[21] => Array
(
[0] => 5
[1] => 4
[2] => 1
)
[22] => Array
(
[0] => 5
[1] => 4
[2] => 3
)
)

Build an array from 3 arrays with PHP

I've some problem to construct an array.
Array A:
Array
(
[0] => 2015-09-13
[1] => 2015-09-14
[2] => 2015-09-15
[3] => 2015-09-16
[4] => 2015-09-17
[5] => 2015-09-18
[6] => 2015-09-19
)
Array B:
Array
(
[0] => 1
[1] => 8
)
Array C:
Array
(
[0] => Leaves-19
[1] => Shifts-18
[2] => Shifts-18
[3] => Shifts-18
[4] => Shifts-18
[5] => Shifts-18
[6] => Leaves-19
[7] => Leaves-19
[8] => Shifts-12
[9] => Shifts-12
[10] => Shifts-12
[11] => Shifts-12
[12] => Shifts-12
[13] => Leaves-19
)
Desired final output:
Array
(
[0] => 2015-09-13|1|Leaves-19
[1] => 2015-09-14|1|Shifts-18
[2] => 2015-09-15|1|Shifts-18
[3] => 2015-09-16|1|Shifts-18
[4] => 2015-09-17|1|Shifts-18
[5] => 2015-09-18|1|Shifts-18
[6] => 2015-09-19|1|Leaves-19
[7] => 2015-09-13|8|Leaves-19
[8] => 2015-09-14|8|Shifts-12
[9] => 2015-09-15|8|Shifts-12
[10] => 2015-09-16|8|Shifts-12
[11] => 2015-09-17|8|Shifts-12
[12] => 2015-09-18|8|Shifts-12
[13] => 2015-09-19|8|Leaves-19
)
I'm lost in for and foreach.
Here's the logic:
1st parameter is a date and it come's form array B. It is repeat
after 6 entries.
2nd parameter is the user id. It changes after 6 entries and pass to the next id.
3rd parameter is an entry of array B.
Oter informations:
The arrays don't have the same length.
Array A, counts 6 entries.
Array B, counts a random entries.
Array C, is Array A x 2.
I already tried to make a for for my array B and after a foreach in array A, but it wasn't functional.
I do not know where I need to start.
Hope I will have any help or cue.
Thanks a lot.
you can use modulus operator
$OutputArray = Array();
for($i=0; $i < max(count($a1),count($a2),count($a3)); $i++){
array_push($OutputArray, $a1[ $i % count($a1) ] . "|" .
$a2[ $i % count($a2) ] . "|" . $a3[ $i % count($a3) ]);
}
print_r($OutputArray);
you get:
Array
(
[0] => 2015-09-13|1|Leaves-19
[1] => 2015-09-14|8|Shifts-18
[2] => 2015-09-15|1|Shifts-18
[3] => 2015-09-16|8|Shifts-18
[4] => 2015-09-17|1|Shifts-18
[5] => 2015-09-18|8|Shifts-18
[6] => 2015-09-19|1|Leaves-19
[7] => 2015-09-13|8|Leaves-19
[8] => 2015-09-14|1|Shifts-12
[9] => 2015-09-15|8|Shifts-12
[10] => 2015-09-16|1|Shifts-12
[11] => 2015-09-17|8|Shifts-12
[12] => 2015-09-18|1|Shifts-12
[13] => 2015-09-19|8|Leaves-19
)
if you want in order (expected):
$OutputArray = Array();
$max = max(count($a1),count($a2),count($a3));
for($i=0; $i < $max; $i++){
array_push($OutputArray, $a1[$i%count($a1)] . "|" .
$a2[ $i*count($a2) / $max ] . "|" . $a3[$i%count($a3)]);
}
print_r($OutputArray);
you get:
Array
(
[0] => 2015-09-13|1|Leaves-19
[1] => 2015-09-14|1|Shifts-18
[2] => 2015-09-15|1|Shifts-18
[3] => 2015-09-16|1|Shifts-18
[4] => 2015-09-17|1|Shifts-18
[5] => 2015-09-18|1|Shifts-18
[6] => 2015-09-19|1|Leaves-19
[7] => 2015-09-13|8|Leaves-19
[8] => 2015-09-14|8|Shifts-12
[9] => 2015-09-15|8|Shifts-12
[10] => 2015-09-16|8|Shifts-12
[11] => 2015-09-17|8|Shifts-12
[12] => 2015-09-18|8|Shifts-12
[13] => 2015-09-19|8|Leaves-19
)
Try this, it iterates over the biggest array and condition the counting of the minor ones to its desired listing behavior.
<?php
$arrA = ['2015-09-13','2015-09-14','2015-09-15','2015-09-16',
'2015-09-17','2015-09-18','2015-09-19'];
$arrB = [1,8];
$arrC = ['Leaves-19','Shifts-18','Shifts-18','Shifts-18','Shifts-18','Shifts-18',
'Leaves-19','Leaves-19','Shifts-12','Shifts-12','Shifts-12','Shifts-12','Shifts-12',
'Leaves-19'];
$a = $b = 0;
for ($c = 0; $c < count($arrC); $c++) {
$arrC[$c] = $arrA[$a].'|'.$arrB[$b].'|'.$arrC[$c];
$a++;
if ($a == count($arrA)) {
$a = 0;
$b++;
}
};
echo "<pre>";
print_r($arrC);
OUTPUT:
Array
(
[0] => 2015-09-13|1|Leaves-19
[1] => 2015-09-14|1|Shifts-18
[2] => 2015-09-15|1|Shifts-18
[3] => 2015-09-16|1|Shifts-18
[4] => 2015-09-17|1|Shifts-18
[5] => 2015-09-18|1|Shifts-18
[6] => 2015-09-19|1|Leaves-19
[7] => 2015-09-13|8|Leaves-19
[8] => 2015-09-14|8|Shifts-12
[9] => 2015-09-15|8|Shifts-12
[10] => 2015-09-16|8|Shifts-12
[11] => 2015-09-17|8|Shifts-12
[12] => 2015-09-18|8|Shifts-12
[13] => 2015-09-19|8|Leaves-19
)
My version:
$arrayA = array('2015-09-13','2015-09-14','2015-09-15','2015-09-16','2015-09-17','2015-09-18','2015-09-19');
$arrayB = array(1,8);
$arrayC = array('Leaves-19','Leaves-18','Leaves-17','Leaves-16','Leaves-15','Leaves-14','Leaves-13','Leaves-12','Leaves-11','Leaves-10','Leaves-9','Leaves-8','Leaves-7','leaves-6');
$output = array();
$count = 0;
foreach($arrayB as $arrayBElement){
foreach($arrayA as $arrayAElement){
$output[] = $arrayC[$count] . '|' . $arrayAElement . '|' . $arrayBElement;
$count++;
}
}
var_dump($output);
E: removed erroneous count
This stinks of a homework problem so I'm going to answer in pseudocode so hopefully you can apply some hints and come to the solution yourself.
It appears that maybe Array B ([1,8]) is a list of indexes at which to start printing out the next index. Your own statement (2nd parameter is the user id. It changes after 6 entries and pass to the next id.) is not accurate based on your desired final output:
[6] => 2015-09-19|1|Leaves-19
[7] => 2015-09-13|8|Leaves-19
This moves to the next id at index 7 (count 8) not index 6 (count 7, or one after 6 entries).
So My pseudo solution would be this:
Create a new array by repeating Array A until it matches the length of Array C
Append Array B to Array A by finding the element of Array B that is equal to or less than (1+index):
0 => '1', 1 => '1' ... 7 => '8'
Concatenate Array C to every array with index parity (eg. newArray[ 0 ] . ArrayC[ 0 ])
This gives you the final output you desire.
try this code, I have named your 3 arrays as $arrA, $arrB and $arrC
$arrA = Array(
'2015-09-13',
'2015-09-14',
'2015-09-15',
'2015-09-16',
'2015-09-17',
'2015-09-18',
'2015-09-19'
);
$arrB = array(1,8);
$arrC = array(
'Leaves-19',
'Shifts-18',
'Shifts-18',
'Shifts-18',
'Shifts-18',
'Shifts-18',
'Leaves-19',
'Leaves-19',
'Shifts-12',
'Shifts-12',
'Shifts-12',
'Shifts-12',
'Shifts-12',
'Leaves-19'
);
$output = array();
$indx = 0;
foreach($arrB as $b){
foreach($arrA as $a){
$output[] = $a.'|'.$b.'|'.$arrC[$indx];
$indx++;/*new line addded*/
}
//$indx++;/*remove this line from here*/
}
echo "<pre>";
print_r($output);
echo "</pre>";
With different sizes try guess maxlength
//Create arrays
var Adates= ["2015-09-13", "2015-09-14", "2015-09-15"];
var Bnums= ["1", "2", "3"];
var Cnums= ["Leaves-19", "Shifts-18", "Shifts-18"];
//get size
var maxLenght=0;
if(Adates.length>maxLenght) maxLenght=Adates.length;
if(Bnums.length>maxLenght) maxLenght=Bnums.length;
if(Cnums.length>maxLenght) maxLenght=Cnums.length;
//merge and populate
var OutputArray;
for (i = 0; i < maxLenght; i++) {
OutputArray[i]=Adates[i]+"|"+Bnums[i]+"|"+Cnums[i];
}

Sum numbers in array

I have an array of numbers (zipcode-areas), like:
Array (
[0] => 34
[1] => 35
[2] => 36
[3] => 51
[4] => 53
[5] => 54
[6] => 55
[7] => 56
[8] => 57
[9] => 60
[10] => 61
[11] => 63
[12] => 64
[13] => 65
[14] => 66
[15] => 67
[16] => 68
[17] => 69
[18] => 74
[19] => 97
)
I want to sum the numbers in the array like that:
Array (
[0] => 34-36
[1] => 51
[2] => 53-57
[3] => 60-61
[4] => 63-69
[5] => 74
[6] => 97
)
How can I archieve that with PHP?
Logically and with a little help from php you can get the desired output
$zipArr = array ("0" => 34,"1" => 35,"2" => 36,"3" => 51,"4" => 53,"5" => 54,"6" => 55,"7" => 56,"8" => 57,"9" => 60,"10" => 61,"11" => 63,"12" => 64,"13" => 65,"14" => 66,"15" => 67,"16" => 68,"17" => 69,"18" => 74,"19" => 97,);
$range = range(10,100,10);
print_r($zipArr);
$new_arr = [];
foreach($range as $rValue){
$tmpArr = [];
foreach($zipArr as $zipValue){
if($zipValue%$rValue < 10){
$tmpArr[] = $zipValue;
}
}
if(!empty($tmpArr)){
if(count($tmpArr) > 1){
$new_arr[] =($tmpArr[0] ." - ".end($tmpArr));
} else {
$new_arr[] =($tmpArr[0]);
}
}
}
$new_arr = array_unique($new_arr);
print_r($new_arr);
output
Array
(
[0] => 34 - 36
[1] => 60 - 69
[3] => 51 - 57
[5] => 74
[6] => 97
)
Here is the logic to get required output.
$arr = array (34,35,36,51,53,54,55,56,57,60,61,63,64,65,66,67,68,69,74,97);
$sum_arr = array();
$prev_val = 0;
foreach($arr as $key => $val)
{
/* Store initial value in array */
if($key == 0)
{
$sum_arr[] = $val;
}
/* If value is not sequence */
else if(($prev_val+1) != $val)
{
/* Get last index of array and append current value */
$sum_last_idx = count($sum_arr) - 1;
/* Append value only if both values are not equal */
if($sum_arr[$sum_last_idx] != $prev_val)
{
$sum_arr[$sum_last_idx] = $sum_arr[$sum_last_idx] . '-' . $prev_val;
}
$sum_arr[] = $val;
}
/* Retain previous value */
$prev_val = $val;
}
print_r($sum_arr);
Output
Array
(
[0] => 34-36
[1] => 51
[2] => 53-57
[3] => 60-61
[4] => 63-69
[5] => 74
[6] => 97
)

Generating permutations from arrays

I tried to port a function that generates permutations from this answer to PHP and I came out with the following:
function recurse($s, $arrs, $k) {
if ($k === count($arrs)) {
echo $s.' ';
} else {
foreach ($arrs[$k] as $o) {
recurse($s.$o, $arrs, $k + 1);
}
}
}
which gives me the correct output 137 138 147 148 237 238 247 248
Now I want to have the output as an array and not as a string, but after the edit for some reason I get wrong results:
function generatePermutations($s, $arrs, $k) {
if ($k === count($arrs)) {
print_r($s);
} else {
foreach ($arrs[$k] as $o) {
$s[] = $o;
generatePermutations($s, $arrs, $k + 1);
}
}
}
Output:
Array
(
[0] => 1
[1] => 3
[2] => 7
)
Array
(
[0] => 1
[1] => 3
[2] => 7
[3] => 8
)
Array
(
[0] => 1
[1] => 3
[2] => 4
[3] => 7
)
Array
(
[0] => 1
[1] => 3
[2] => 4
[3] => 7
[4] => 8
)
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 7
)
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 7
[4] => 8
)
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 7
)
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 7
[5] => 8
)
This is the input for both functions
$in = array( array(1, 2), array(3, 4), array(7, 8) );
recurse("", $in, 0);
generatePermutations(array(), $in, 0);
What did I do wrong?
if you use $s[] = $o; in your recursive function, it will slice your array for every digit.
You can add a "result" parameter, which will hold your resultset as an array.
Notice the & sign. Without it, the parameter is not "writable", only "readable".
$in = array( array(1, 2), array(3, 4), array(7, 8) );
$result = array();
generatePermutations("", $in, 0, $result);
print_r($result);
function generatePermutations($s, $arrs, $k, &$result) {
if ($k === count($arrs)) {
$result[] = $s;
} else {
foreach ($arrs[$k] as $o) {
generatePermutations($s.$o, $arrs, $k + 1, $result);
}
}
}
Output :
Array
(
[0] => 137
[1] => 138
[2] => 147
[3] => 148
[4] => 237
[5] => 238
[6] => 247
[7] => 248
)
Edit : Modification to get Robotex's expected output :
$in = array( array(1, 2), array(3, 4), array(7, 8) );
$result = array();
$s = array();
generatePermutations($s, $in, 0, $result);
print_r($result);
function generatePermutations(&$s, $arrs, $k, &$result) {
if ($k === count($arrs)) {
array_push($result, $s);
} else {
foreach ($arrs[$k] as $o) {
array_push($s, $o);
generatePermutations($s, $arrs, $k + 1, $result);
array_pop($s);
}
}
}
Output :
Array
(
[0] => Array
(
[0] => 1
[1] => 3
[2] => 7
)
[1] => Array
(
[0] => 1
[1] => 3
[2] => 8
)
[2] => Array
(
[0] => 1
[1] => 4
[2] => 7
)
[3] => Array
(
[0] => 1
[1] => 4
[2] => 8
)
[4] => Array
(
[0] => 2
[1] => 3
[2] => 7
)
[5] => Array
(
[0] => 2
[1] => 3
[2] => 8
)
[6] => Array
(
[0] => 2
[1] => 4
[2] => 7
)
[7] => Array
(
[0] => 2
[1] => 4
[2] => 8
)
)

Categories