I have a dynamically created array $array=[2,3,2]. I want to loop through it and get all the possible permutations.
$array = [2,3,2];
$c = count($array);
for($i=0; $i<$c; $i++) {
for($j=0; $j<$array[$i]; $j++) {
echo ($i+1).'-'.($j+1).'<br>';
}
}
The result should be something like:
1-1-1
1-1-2
1-2-1
1-2-2
1-3-1
1-3-2
2-1-1
2-1-2
2-2-1
2-2-2
2-3-1
2-3-2
but it returns that:
1-1
1-2
2-1
2-2
2-3
3-1
3-2
I hope this ll help you :
$array = [2, 3, 2];
for ($i = 1; $i <= $array[0]; $i++) {
for ($j = 1; $j <= $array[1]; $j++) {
for ($k = 1; $k <= $array[2]; $k++) {
echo $i,"-",$j,"-",$k,"<br>";
}
}
}
Related
I need to create two-dimensional array by using 2 loops.
Array must look like this: [[1, 2, 3], [4, 5, 6], [7, 8, 9]];
This is what I tried, but I wanted to see better solution and to know is my solution bad.
<?php
$arr = [];
$elem = 1;
for ($i = 0; $i <= 2; $i++) {
for ($j = 1; $j <= 3; $j++) {
$arr[$i][] = $elem++;
}
}
?>
$number = range(1,9);
print_r (array_chunk($number,3));
Others have shown you some clever ways, but keeping it simple in case you are just starting out with programming.... In the inner loop, create a temporary array, then outside the inner loop but inside the outer, add it to your main array.
$arr = [];
$elem = 1;
for ($i = 0; $i <= 2; $i++) {
$t = []; #Init empty temp array
for ($j = 1; $j <= 3; $j++) {
$t[] = $elem++;
}
$arr[] = $t;
}
One of hundreds options:
$arr = [
range(1, 3),
range(4, 6),
range(7, 9),
];
print_r($arr);
one method: this method use "temp variables".
<?php
$arr_inner = [];
$arr_main = [];
$elem=1;
for ($i = 0; $i <= 2; $i++) {
for ($j = 0; $j <= 2; $j++) {
$elem =$elem +1;
$arr_inner[$j] = $elem;
}
$arr_main[$i] = $arr_inner;
unset($arr_inner);
}
?>
How do I return all possible combinations [12345], [12354] up to [54312], [54321] without having to run 120 for...loop as in the case of combining a 2-item array in the code below?
To return all possible combinations from the given array $word = [1,2],
//break the array into 2 separate arrays
$arr1 = $word[0]; $arr2 = $word[1];
//computer for first array item...each item will have 2 loops
for($i=0; $i<count($arr1); $i++){
for($j=0; $j<count($arr2); $j++){
$ret = $arr1[$i] . $arr2[$j]; array_push($result, $ret);
}
}
//computer for second array item..each item will have 2 loops
for($i=0; $i<count($arr2); $i++){
for($j=0; $j<count($arr1); $j++){
$ret = $arr2[$i] . $arr1[$j]; array_push($result, $ret);
}
}
//display the result
for ($i = 0; $i < count($result); $i++){
echo result([$i];
}
The above code works well.
But for a 5-item array [1,2,3,4,5], it will require about (5 items * 24 loops) = 120 loops.
As seen, you wanted to split 2 strings into chars and obtain all combination by 2 chars: first form blank1 and second from blank2.
Instead of doing the combination manually use a regular for-loop.
$result = array();
for ($i = 0; $i < count($blank1); $i++)
{
for ($j = 0; $j < count($blank2); $j++)
{
//set combination
$aux = $blank1[$i].$blank2[$j];
array_push($result, $aux);
}
}
//result should be populated with combination of 2
//just list it and use as need
for ($i = 0; $i < count($result); $i++)
{
echo $result[$i];
}
//same with stored or checking on db : use loops
For multiple combination, use more nested loops
eg: [blank1][blank2][blank1] - 3 combination
$result = array();
//1
for ($i = 0; $i < count($blank1); $i++)
{
//2
for ($j = 0; $j < count($blank2); $j++)
{
//3
for ($k = 0; $k < count($blank1); $k++)
{
//set combination
$aux = $blank1[$i].$blank2[$j].$blank1[$k];
array_push($result, $aux);
}
}
}
Same as any number you wanted ! It will be a little annoying if have to write many loops but note while can be used with an adequate algorithm. But for the moment just keep as simple as you can and get the desired result.
hi i am using php to learn algorithms, i wanted to convert this psuedocode into php,
for i = 1 to n − 1
minval = A[i]
minindex = i
for j = i to n
if (A[j] < minval)
minval = A[j]
minindex = j
exchange A[i] and A[minindex]
this the corresponding code in php
$A = array(1, 4, 2, 3, 70, 10, 7 );
$n = sizeof($A);
for ($i = 0; $i == $n - 1; $i++){
for ($j = $i + 1; $j == $n; $j++){
if ($A[$i] > $A[$j]){
$temp = $A[$j];
$A[$j] = $A[$i];
$A[$i] = $temp;
}
}
}
print_r($A);
print_r is outputting the array as its original order, why my algorithms doents reorder the array ?
You should check your forloops :
for ($i = 0; $i == $n - 1; $i++){
for ($j = $i + 1; $j == $n; $j++){
should be
for ($i = 0; $i < $n - 1; $i++){
for ($j = $i + 1; $j < $n; $j++){
As the second argument in for is a requirement to continue the loop.
I have code like this
$jumlahcolspan = array();//new array
$horizontaldeep = 5;
$level = array(5,4,3,8,7);//old array
for ($j = 0; $j < $horizontaldeep; $j++) {
$jml = 1;
for ($i = $j + 1; $i < $horizontaldeep; $i++) {
$jml = $level[$i] * $jml;
}
array_push($jumlahcolspan, $jml);
}
To put it simple, what I want to get is to multiply old array value which index start from $i+1 to the last and push it to another array.
So, its some thing like this
old array: [5, 4, 3, 8, 7]
new array: [4*3*8*7, 3*8*7, 7, 1]
I've tried this but it doesn't work also
for ($j = 0; $j < $horizontaldeep; $j++) {
$jml = 1;
for ($i = $j + 1; $i < $horizontaldeep; $i++) {
global $jml;
$jml = $level[$i] * $jml;
}
array_push($jumlahcolspan, $jml);
}
Tried this too but not work also.
for ($j = 0; $j < $horizontaldeep; $j++) {
array_push($jumlahcolspan, array_product(array_slice($level, $j+1)));
}
Note: now I'm reviewing my full code. May be something not right in my code.
I think the problem is related to $jml variable but I can't figure how to solve that. Can anyone help me?
One approach would be to use a Recursive Function to achieve that goal. The Recursive Function below demonstrates how. And, by the way, you may as well quick-test it here.
<?php
$oldArray = [5,4,3,8,7];
function arrayMatrixMultiply(array $old, array &$newArray=[]){
$result = 1;
foreach($old as $key=>$value){
if($key != 0){
$result*=$value;
}
}
$newArray[] = $result;
array_splice($old, 0, 1);
if(!empty($old)){
// JUST RECURSE TILL THE $oldArray BECOMES EMPTY
arrayMatrixMultiply($old, $newArray);
}
return $newArray;
}
$newArray = arrayMatrixMultiply($oldArray);
var_dump($newArray);
// PRODUCES::
array (size=5)
0 => int 672
1 => int 168
2 => int 56
3 => int 7
I want to save value from looping in an array. Then I want to sort it to numerically desc.
here is my code
<?php
$n = 5;
for ($i = 0 ; $i < $n ; $i++){
$a = $i + 1;
echo $a;
} // the result is 1,2,3,4,5. How can I do sorting so the result will be 5,4,3,2,1?
?>
Simply flip the loop:
<?php
$n = 5;
// the for loop now starts at $n and will decrease $i by 1 after every loop
for ($i = $n ; $i > 0 ; $i--){
echo $i;
} // the result is 5,4,3,2,1
?>
function desc($n){
$arr = array();
for ($i = 1 ; $i <= $n ; $i++){
array_push($arr,$i);
}
$arr = array_reverse($arr);
foreach ($arr as $key => $value) {
echo $value . " => ";
}
}
desc(5); // output 5 => 4 => 3 => 2 => 1
PS. In this way you are saving values in array as well
<?php
$array = array();
$n = 5;
for ($i = 0 ; $i < $n ; $i++){
array_push($array,($i+1));
}
$array = array_reverse($array);
for($i = 0 ; $i < sizeof($array) ; $i++)
echo $array[$i].'<br>';
?>