PHP For Loop N Times Based On Array Length - php

I have this array :
$products = array('0', '1', '2', '3');
and I want to have this combination :
0
0,1
0,1,2
0,1,2,3 // my code can't show this result
0,1,3 // my code can't show this result
0,2
0,2,3
0,3
1
1,2
1,2,3
1,3
2
2,3
3
and here's my code :
$rows = count($products);
for ($i = 0; $i < $rows; $i++) {
echo $i . '<br>';
for ($j = $i + 1; $j < $rows; $j++){
echo $i . $j . '<br>';
if (!empty($products[$j+1])) {
echo $i . $j . $products[$j+1] . '<br>';
}
}
}
but, unfortunately I missed 2 results :
0,1,2,3
0,1,3
and I still have doubt about this for loop inside loop, while the number of combination is determined by array length.
how to get that missed combination and perfectly works for different array length?

$products = array('0', '1', '2', '3');
$rows = count($products); //missing here array count......
for ($i = 0; $i < $rows; $i++) {
echo $i . '<br>';
for ($j = $i + 1; $j < $rows; $j++){
echo $i . $j . '<br>';
if (!empty($products[$j+1])) {
echo $i . $j . $products[$j+1] . '<br>';
}
}
}

Try this
<?php
$set = array('0', '1', '2','3');
$power_set = possible_set($set);
echo "<pre>";
print_r($power_set);
echo "</pre>";
function possible_set($array) {
$results = array(array( ));
foreach ($array as $element)
foreach ($results as $inner_element)
array_push($results, array_merge(array($element), $inner_element));
unset($results[0]);
$results = array_values($results);
return $results;
}
?>
https://eval.in/516963

Related

max() function can't find correct value. php

I'm trying to get the max value in the array.
But using max() for the entire array it can't find the highest value:
The return Key is 2, which is not the correct key, obviously the highest value is in array(3).
Please help me to find my mistake.
Here bellow is my code:
<?php
$hex_splited = [
['00','00','00'],
['10', '11', '10'],
['F0', '1A', 'C3'],
['0F', 'FE', 'F4'],
];
$arr_rgb = [];
$count2 = count($hex_splited);
for ($i=0; $i < $count2; $i++) {
$inn_count = count($hex_splited[$i]);
for ($j=0; $j < $inn_count; $j++) {
$val = hexdec($hex_splited[$i][$j]);
$arr_rgb[$i][$j] = $val;
}
}
$max = max($arr_rgb);
var_dump($max);
$key = array_search($max, $arr_rgb);
echo "<pre>";
echo "array key is: " . $key;
echo "</pre>";
echo "<table border='1'>";
for ($m=0; $m < $count2; $m++) {
echo "<tr>";
for ($k=0; $k < $inn_count; $k++) {
echo "<td>";
echo $arr_rgb[$m][$k];
echo "</td>";
}
echo "</tr>";
}
echo "</table>";
The problem is that max() doesn't always behave as you would expect when using multidimensional arrays.
This code does the processing in two stages, first finds the max value. As this is only a 2 dimensional simple array, you can use...
$max = max(array_map("max", $hex_splited));
This applies max to each of the sub arrays and then gets the max of those values.
So in your case, this will be 'FE'. Then as part of the loop which converts the data to decimal, it compares the original value with the max already found and stores the key...
$key = 0;
for ($i=0; $i < $count2; $i++) {
$inn_count = count($hex_splited[$i]);
for ($j=0; $j < $inn_count; $j++) {
$val = hexdec($hex_splited[$i][$j]);
$arr_rgb[$i][$j] = $val;
// Check if found the max value
if ( $max == $hex_splited[$i][$j] ) {
$key = $i;
}
}
}

Calculate sum in while loop to array

When the number 6 is giving I need to calculate till 6: for example: 1+2+3+4+5+6=21. I also need to add the sums of each to an array like: 1+2=3, 1+2+3=6, 1+2+3+4=10, ...
I have tried to make the while loop to be printed and this working fine but nog for array purpose:
<?php
$number = $_POST['number'];
$i = 1;
$cal = 0;
$tussenBerekening = array();
while ($i <= $number) {
echo $i;
$cal = $cal + $i;
array_push($tussenBerekening, $cal);
if ($i != $number) {
echo " + ";
} else {
echo " = " . $cal;
}
$i++;
}
?>
This is my new code, it prints, but no total sum.
<?php
$number = $_POST['number'];
$i = 2;
$cal = 0;
$sum = 1;
$berekeningen = array();
while ($i <= $number) {
$sum .= "+" . $i;
array_push($berekeningen, $sum);
$i++;
}
print_r($berekeningen);
?>
Here's a solution:
$i = 1;
$number = 6;
while ($i <= $number) {
// generate array with values from 1 to $i
$array = range(1, $i);
// if there're more than 1 element in array - output sum
if (count($array) > 1) {
// 1+2+... part // sum of elements of array
echo implode('+', $array) . ' = ' . array_sum($array) . '<br />';
}
$i++;
}

php echo data to html performance

I add some Data in form of a table to my website. Actually everything works but i wanted to test the performance for some big data. It was very bad. Here my Results:
tablesize | time
-----------------
10x20 | 0.22s
100x20 | 3.29s
1000x20 | 28.75s
My question is which way is better to show the results and why:
1:
<?php
for($i=0; $i<count($array); $i++){
echo "<div id='...' class='...Ä style='...'>"
for($i=0; $i<count($array); $i++){
echo "<div style='...'>".array[$i][$j]."</div>";
}
echo "</div>";
}
?>
2:
<?php
for($i=0; $i<count($array); $i++){
?>
<div id='...' class='...Ä style='...'>
<?php
for($i=0; $i<count($array); $i++){
?>
<div style='...'> <?php echo array[$i][$j]; ?> </div>
<?php } ?>
</div>
<?php } ?>
Maybe someone has some additional tips how i can make the data visualization fast. I read some artical about MySQL structure and i believe my DB looks fine. So what i can do to optimize the reading of data.
As scaisEdge said, paginate your data.
Moreover, there are several changes that can make your code faster.
count($foo) outside of for loop
use pre-increment instead of post
use 'str' and concatenation instead of "str"
Here is a "benchmark" (without output) :
<?php
$array = array();
for($i = 0; $i < 1000; ++$i){
$array[] = range(0, 20, 1);
}
$t = microtime(true);
for($k = 0; $k < 5000; ++$k){
for($i=0; $i<count($array); $i++){
$a = "<div id='...' class='...Ä style='...'>";
for($j=0; $j<count($array[$i]); $j++){
$a = "<div style='...'>".$array[$i][$j]."</div>";
}
$a = "</div>";
}
}
echo 'Count and post : ' . (microtime(true) - $t) . "\n";
$t = microtime(true);
for($k = 0; $k < 5000; ++$k){
$c = count($array);
for($i=0; $i<$c; $i++){
$a = "<div id='...' class='...Ä style='...'>";
$c2 = count($array[$i]);
for($j=0; $j<$c2; $j++){
$a = "<div style='...'>".$array[$i][$j]."</div>";
}
$a = "</div>";
}
}
echo 'No count and post : ' . (microtime(true) - $t) . "\n";
$t = microtime(true);
for($k = 0; $k < 5000; ++$k){
$c = count($array);
for($i=0; $i<$c; ++$i){
$a = "<div id='...' class='...Ä style='...'>";
$c2 = count($array[$i]);
for($j=0; $j<$c2; ++$j){
$a = "<div style='...'>".$array[$i][$j]."</div>";
}
$a = "</div>";
}
}
echo 'No count and pre : ' . (microtime(true) - $t) . "\n";
$t = microtime(true);
for($k = 0; $k < 5000; ++$k){
$c = count($array);
for($i=0; $i<$c; ++$i){
$a = '<div id="..." class="...Ä style="...">';
$c2 = count($array[$i]);
for($j=0; $j<$c2; ++$j){
$a = '<div style="...">'.$array[$i][$j].'</div>';
}
$a = '</div>';
}
}
echo 'No count and pre and \' : ' . (microtime(true) - $t) . "\n";
// #scaisEdge
$t = microtime(true);
for($k = 0; $k < 5000; ++$k){
foreach($array as $subarray){
$a = '<div id="..." class="...Ä style="...">';
foreach($subarray as $v){
$a = '<div style="...">'.$v.'</div>';
}
$a = '</div>';
}
}
echo 'Foreach : ' . (microtime(true) - $t) . "\n";
?>
This outputs :
Count and post : 46.050459861755
No count and post : 30.590306043625
No count and pre : 29.880299091339
No count and pre and ' : 29.930299043655
Foreach : 29.120290994644
As weird as it looks, ' seems to be slower than ".
PHP checks " strings variables inside and replace it whereas ' string are just... string. No operation performed.
Has someone an idea of this slowness ?
For large output you should use pagination ... output 1000 rows seems not useful
If you paginate your result is more equal to the firts that to the last .
But you can do somethings for the code .. you should count the result just one time and not for all cycle of the loop
so instead of for($i=0; i<count($array); $i++){
you should use
$numElem =count($array);
for($i=0; $i<$numElem; $i++){
.......
}
or use
foreach ( $array as $key => $value ){
......
}
these are much faster

combination of destinations in php

I have destinations array as array('destination1', 'destination2', 'destination3', 'destination4'). I want distinct combinations as
'destination1' => 'destination2'
'destination1' => 'destination3'
'destination1' => 'destination4'
'destination2' => 'destination3'
'destination2' => 'destination4'
what is the simple solution for doing this?
Did you want to do like this
$arr = array('destination1', 'destination2', 'destination3', 'destination4');
$limit = count($arr);
for($i = 0; $i < ($limit - 2); $i++){
for($j = $i + 1; $j < $limit; $j++){
echo $arr[$i].'=>'.$arr[$j].'</br>';
}
}
Improved and optimized version for any size:
while ($curr = array_shift($arr)) foreach ($arr as $item) echo $curr . '=>' . $item . '<br />';

PHP : Save value in array and sort the value to numerically desc?

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>';
?>

Categories