PHP Developer Forum Hier habt ihr die Möglichkeit, eure Skriptprobleme mit anderen Anwendern zu diskutieren. Seid so fair und beantwortet auch Fragen von anderen Anwendern. Dieses Forum ist sowohl für ANFÄNGER als auch für PHP-Profis! Fragen zu Laravel, YII oder anderen PHP-Frameworks. |
 |

12-02-2020, 11:56
|
sanktusm
Registrierter Benutzer
|
|
Registriert seit: Aug 2005
Beiträge: 659
|
|
Combination ausloten und keys beibehalten
Hallo,
ich habe hier ein Funktion, die fast das Problem löst:
PHP-Code:
function getAllCombos($arr) {
$combinations = array();
$words = sizeof($arr);
$combos = 1;
for($i = $words; $i > 0; $i--) {
$combos *= $i;
}
while(sizeof($combinations) < $combos) {
shuffle($arr);
$combo = $arr;
if(!in_array($combo, $combinations)) {
$combinations[] = $combo;
}
}
return $combinations;
}
Diese gibt mir z.B. das aus:
PHP-Code:
array (
0 =>
array (
0 => 2950,
1 => 3900,
),
1 =>
array (
0 => 3900,
1 => 2950,
),
)
Nun möchte ich aber die Reihenfolgen änder on das die Zuordnung von key und value vertrauscht wird also:
PHP-Code:
array (
0 =>
array (
0 => 2950,
1 => 3900,
),
1 =>
array (
1 => 3900,
0 => 2950,
),
)
Kann mir jemand einen Tipp geben, wie das gehen kann?
|

12-02-2020, 13:56
|
sanktusm
Registrierter Benutzer
|
|
Registriert seit: Aug 2005
Beiträge: 659
|
|
Mit diesem Script könnte es besser funktionieren, aber wie...
kann ich die Kombination als Array mit beibehaltenen Keys erhalten?
PHP-Code:
<?php
// Program to print all
// combination of size r
// in an array of size n
// The main function that
// prints all combinations
// of size r in arr[] of
// size n. This function
// mainly uses combinationUtil()
function printCombination($arr,
$n, $r)
{
// A temporary array to
// store all combination
// one by one
$data = array();
// Print all combination
// using temprary array 'data[]'
combinationUtil($arr, $data, 0,
$n - 1, 0, $r);
}
/* arr[] ---> Input Array
data[] ---> Temporary array to
store current combination
start & end ---> Staring and Ending
indexes in arr[]
index ---> Current index in data[]
r ---> Size of a combination
to be printed */
function combinationUtil($arr, $data, $start,
$end, $index, $r)
{
// Current combination is ready
// to be printed, print it
if ($index == $r)
{
for ($j = 0; $j < $r; $j++)
echo $data[$j];
echo "\n";
return;
}
// replace index with all
// possible elements. The
// condition "end-i+1 >=
// r-index" makes sure that
// including one element at
// index will make a combination
// with remaining elements at
// remaining positions
for ($i = $start;
$i <= $end &&
$end - $i + 1 >= $r - $index; $i++)
{
$data[$index] = $arr[$i];
combinationUtil($arr, $data, $i + 1,
$end, $index + 1, $r);
}
}
// Driver Code
$arr = array(1, 2, 3, 4, 5);
$r = 3;
$n = sizeof($arr);
printCombination($arr, $n, $r);
// This code is contributed by ajit
?>
|
Aktive Benutzer in diesem Thema: 1 (Registrierte Benutzer: 0, Gäste: 1)
|
|
Themen-Optionen |
|
Thema bewerten |
|
Forumregeln
|
Es ist Ihnen nicht erlaubt, neue Themen zu verfassen.
Es ist Ihnen nicht erlaubt, auf Beiträge zu antworten.
Es ist Ihnen nicht erlaubt, Anhänge hochzuladen.
Es ist Ihnen nicht erlaubt, Ihre Beiträge zu bearbeiten.
HTML-Code ist aus.
|
|
|
|
PHP News
|