I am trying to figure out a function to get sets of 6 from an array.
I want to get every combination but not the unique combination.
For example if 1,2,3,4,5,6 already exsist 4,5,6,1,2,3 is not needed.
I just want the set of 6 possible combinations that will cover all numbers in the array.
Thank you for the help and your time.
I have tried:
function pc_permute($items, $perms = [], &$ret = []) {
if (empty($items)) {
$ret[] = $perms;
} else {
for ($i = count($items) - 1; $i >= 0; --$i) {
$newitems = $items;
$newperms = $perms;
list($foo) = array_splice($newitems, $i, 1);
array_unshift($newperms, $foo);
$this->pc_permute($newitems, $newperms,$ret);
}
}
return $ret;
}
but this is not what I want
my array is like:
$array = array(1,2,3,4,5,6,7,8,9);
One way to go about this is to shuffle() your array. Then copy the array using array_slice() from 0 - 5, 1 - 6, etc...
$arr = [1,2,3,4,5,6,7,8,9];
shuffle($arr);
$result = [];
for ($x = 0; $x < 6; $x++) {
$temp = array_slice($arr, $x, 6);
if ( count( $temp ) < 6 ) $temp = array_merge( $temp, array_slice($arr, 0, 6 - count( $temp ) ) );
shuffle($temp);
$result[] = $temp;
}
echo "<pre>";
print_r( $result );
echo "</pre>";
Sample output:
Array
(
[0] => Array
(
[0] => 8
[1] => 2
[2] => 7
[3] => 3
[4] => 6
[5] => 5
)
[1] => Array
(
[0] => 3
[1] => 8
[2] => 5
[3] => 7
[4] => 1
[5] => 2
)
[2] => Array
(
[0] => 1
[1] => 3
[2] => 4
[3] => 5
[4] => 2
[5] => 7
)
[3] => Array
(
[0] => 7
[1] => 5
[2] => 9
[3] => 1
[4] => 4
[5] => 3
)
[4] => Array
(
[0] => 4
[1] => 5
[2] => 6
[3] => 7
[4] => 9
[5] => 1
)
[5] => Array
(
[0] => 6
[1] => 8
[2] => 1
[3] => 9
[4] => 7
[5] => 4
)
)
Related
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.
i need a function that transforms a 2d array from:
1 4
2 5
3 6
to
1 2
3 4
5 6
my first approach was by transforming the matrix with
array_unshift($data, null);
call_user_func_array('array_map', $data);
But now i got:
1 5
2 6
3
4
Can someone help?
Edit:
Ok let me be more clear i have these categories and i need to reorder them in this way
Array
(
[0] => Array
(
[0] => Das ist los
[1] => Land & Leute
[2] => Wirtschaft & Politik
[3] => Leben
[4] => Kultur
)
[1] => Array
(
[0] => Chronik
[1] => Motor
[2] => Sport
[3] => Blaulicht
[4] => Schauplatz
)
)
I need:
Array
(
[0] => Array
(
[0] => Das ist los
[1] => Wirtschaft & Politik
[2] => Kultur
[3] => Motor
[4] => Blaulicht
)
[1] => Array
(
[0] => Land & Leute
[1] => Leben
[2] => Chronik
[3] => Sport
[4] => Schauplatz
)
)
For a simple 2-column array:
$myArray = [
[1,4],
[2,5],
[3,6],
];
$myNewArray = array_chunk(
array_merge(
array_column($myArray, 0),
array_column($myArray, 1)
),
2
);
var_dump($myNewArray);
EDIT
For a more generic solution:
$myArray = [
[1,6,11],
[2,7,12],
[3,8,13],
[4,9,14],
[5,10,15],
];
$columns = count($myArray[0]);
$tmpArray = [];
for($i = 0; $i < $columns; ++$i) {
$tmpArray = array_merge(
$tmpArray,
array_column($myArray, $i)
);
}
$myNewArray = array_chunk(
$tmpArray,
$columns
);
var_dump($myNewArray);
I have a array like this and want to sort it so that no record repeated till it have more than one distinc values in it.
Array
(
[0] => 1
[1] => 1
[2] => 1
[3] => 1
[4] => 1
[5] => 2
[6] => 2
[7] => 2
[8] => 4
[9] => 4
)
I want the result array like this.
Array
(
[0] => 1
[1] => 2
[2] => 4
[3] => 1
[4] => 2
[5] => 4
[6] => 1
[7] => 2
[8] => 1
[9] => 1
)
Thanks In advance..
This should work for you:
<?php
$array = array (
0 => 1,
1 => 1,
2 => 1,
3 => 1,
4 => 1,
5 => 2,
6 => 2,
7 => 2,
8 => 4,
9 => 4,
);
$newArray = array();
$i = 0;
while (count($array) > 0) {
$newArray[$i] = array();
foreach ($array as $key => $value) {
if (!in_array($value, $newArray[$i])) {
array_push($newArray[$i], $value);
unset($array[$key]);
}
}
sort($newArray[$i]);
$i++;
}
$index = 0;
for ($j = 0; $j < count($newArray); $j++) {
foreach ($newArray[$j] as $value) {
$array[$index] = $value;
$index++;
}
}
?>
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
)
)
This question already has answers here:
How to Flatten a Multidimensional Array?
(31 answers)
Closed 9 years ago.
I have a miltidimentional array like this:
Array ( [11] => Array ( [0] => 5 )
[12] => Array ( [0] => 7 )
[14] => Array ( [0] => 9 [1] => 10 [2] => 11 [3] => 25 )
)
And would like to convert it like that:
Array ( [0] => Array ( [11] => 5 )
[1] => Array ( [12] => 7 )
[2] => Array ( [14] => 9 )
[3] => Array ( [14] => 10)
[4] => Array ( [14] => 11)
[5] => Array ( [14] => 25)
)
Do you have any idea how it could be done?
Bare minimum:
foreach($myArray as $a => $b)
foreach($b as $c)
$tmpArray[] = array($a => $c);
$myArray = $tmpArray; unset($tmpArray);
Working Example
<pre>
<?php
$arr = array();
$arr[11] = array(0, 1, 2);
$arr[12] = 3;
$arr[13] = array(4, 5);
print_r($arr);
$arr2 = array();
$x = 0;
$a = 0;
while ($x < 14) {// count() will fail with '3' but our numbers are at 11
if (is_array($arr[$x])) {
$p = 0;
while ($p < count($arr[$x])) {// works here because index starts at 0
if (!is_null($arr[$x])) {
$arr2[$a++] = $arr[$x][$p];
}
$p ++;
}
}
else {
if (!is_null($arr[$x])) {
$arr2[$a++] = $arr[$x];
}
}
$x ++;
}
print_r($arr2);
?>
</pre>
Result:
Array
(
[11] => Array
(
[0] => 0
[1] => 1
[2] => 2
)
[12] => 3
[13] => Array
(
[0] => 4
[1] => 5
)
)
Array
(
[0] => 0
[1] => 1
[2] => 2
[3] => 3
[4] => 4
[5] => 5
)
im too tired so this is the max i can do now..
The problems:
while ($x < 14) {// count() will be '3' but our numbers are at 11
Maybe this code could be improved..