I've created a for loop, but it does not print out the output according to the for loop conditions.
for( $c=0; $c < $total; $c++ )
{
$latestID = AccessControlEntry::orderBy('AccessControlID', 'desc')->first();
$numberID = explode("AC", $latestID->AccessControlID);
$numberID[1] = $numberID[1] + 1;
$newID = "AC";
$newID = $newID.$numberID[1];
}
I grabbed the last ID value (AccessControlID) from the database, and subsequently add 1 ($numberID[1] + 1) for every new entry in AccessControlID.
For eg: $total = 3.
From my understanding of for-loops logic, shouldn't it print out 2 sets of $newID?
Can anyone tell me where/what did I went wrong?
The solution is to bring outside the loop the lines of code which extract out from the DB the value.
$latestID = AccessControlEntry::orderBy('AccessControlID', 'desc')->first();
$numberID = explode("AC", $latestID->AccessControlID);
for( $c=0; $c < $total; $c++ ) {
$numberID[1] = $numberID[1] + 1;
$newID[] = $numberID[1];
}
print_r($newID);
Related
I want to calculate an array to another array and then add both like this :
Array1(4,8,7,12);
Array2(3,6);
the result is like this :
3x4+3x8+3x7+3x12+6x4+6x8+6x7+6x12 = 279
I tried with this code and since I'm still new with php I still didn't make any tips I'll be glad thanks in advance
<?php
tab1(4,8,7,12);
tab2(3,6);
$s=0;
for($i=0; $i<$tab1[3]; $i++){
for($j=0; $j<$tab2[1]; $j++){
$s = $s + tab[i] * tab[j];
}
echo "$s";
}
?>
This is actually a piece of cake. I am not sure what tab1 means but I assume you are trying to use this function to get an array of desired(input) numbers.
Using a foreach loop to loop over the two arrays, I came up with the code below:
<?php
$a = [4,8,7,12];
$b = [3,6];
$sum = 0;
foreach ($b as $valueInB) {
foreach ($a as $valueInA) {
$sum += $valueInA * $valueInB;
}
}
echo $sum;
?>
which results in:
279
if you are insisting on using a for loop you can run the code below to get the same result:
<?php
$a = [4,8,7,12];
$b = [3,6];
$sum = 0;
$bLength = count($b);
$aLength = count($a);
for ($i=0; $i < $bLength ; $i++) {
$valueInB = $b[$i];
for ($j=0; $j < $aLength ; $j++) {
$valueInA = $a[$j];
$sum += $valueInA * $valueInB;
}
}
echo $sum;
?>
Please note that you should use the count outside of the loop because looping over the array and calling count each time is not an efficient way. (Thanks to Will B for the comment)
For a more simplistic approach without iteration you can use array_sum on the two arrays.
Example: https://3v4l.org/3gtgE
$a = [4,8,7,12];
$b = [3,6];
$c = array_sum($a) * array_sum($b);
var_dump($c);
Result
int(279)
This works because of the order of operations of:
(3*4) + (3*8) + (3*7) + (3*12) + (6*4) + (6*8) + (6*7) + (6*12) = 279
Equates to the same as the sum of a multiplied by the sum of b:
(4+8+7+12) * (3+6) = 279
i have this function:
function pairwiseDifference($arry)
{
$n = count($arry) - 1; // you can add the -1 here
$diff = 0;
$result = array();
for ($i = 0; $i < $n; $i++)
{
// absolute difference between
// consecutive numbers
$diff = abs($arry[$i] - $arry[$i + 1]);
echo $diff." ";
array_push($result, $diff); // use array_push() to add new items to an array
}
return $result; // return the result array
}
$arry = array(20,10,10,50);
echo "<br> Percent of commisions are: ";
// this echos 10,0,40 and assigns whatever your function returns
// to the variable $diffArray
$diffArray = pairwiseDifference($arry);
The problem are that im not expecting this output
becouse first number of array (20) is my commission
and the other numbers are my parents commission (10,10,50).
So basically i need to output like this: (0,0,30)
becouse i take 20 of commission,
first parent not take nothing becouse are less of my commission (10)
second parent not take nothing becouse are less of my commission (10)
and only last parent take 30 becouse are greater than my commission (50 - 20 my commission).
Thanks in advance
Since your first element of the array is your commission and the others are the the commissions of parents, and since it seems that you don't want to include your commission in the result array, you can do something like this:
function pairwiseDifference($arry)
{
$n = count($arry);
$diff = 0;
$result = array();
$myComm = $arry[0]; // your commision
for ($i = 1; $i < $n; $i++)
{
$diff = 0; // set $diff to 0 as default
if($myComm < $arry[$i]) // if your commision < parent commision
$diff = $arry[$i] - $myComm;
echo $diff." ";
array_push($result, $diff);
}
return $result;
}
$arry = array(20,10,10,50);
echo "<br> Percent of commisions are: ";
$diffArray = pairwiseDifference($arry);
echo $diffArray[0]; // prints 0
echo $diffArray[1]; // prints 0
echo $diffArray[2]; // prinst 30
To tweak the logic according to your code, there would be only 3 modifications.
Create a $max variable and assign it the value of $arry[0].
Make difference as 0 if current max is greater than current one, else take the difference.
Calculate the new max again using max() function.
Snippet:
<?php
function pairwiseDifference($arry)
{
$n = count($arry) - 1; // you can add the -1 here
$diff = 0;
$result = array();
$max = $arry[0]; // new addition
for ($i = 1; $i <= $n; $i++) // new modification <= n instead of < n
{
// absolute difference between
// consecutive numbers
$diff = $max < $arry[$i] ? $arry[$i] - $max : 0; // new modification
$max = max($max, $arry[$i]); // new modification
echo $diff." ";
array_push($result, $diff); // use array_push() to add new items to an array
}
return $result; // return the result array
}
$arry = array(20,10,10,50);
echo "<br> Percent of commisions are: ";
// this echos 10,0,40 and assigns whatever your function returns
// to the variable $diffArray
$diffArray = pairwiseDifference($arry);
So, I want to distribute evenly lists across 3 columns. The lists cannot be broken up or reordered.
At the moment, I have 5 lists each containing respectively 4, 4, 6, 3 and 3 items.
My initial approach was:
$lists = [4,4,6,3,3];
$columns = 3;
$total_links = 20;
$items_per_column = ceil($total_links/$columns);
$current_column = 1;
$counter = 0;
$lists_by_column = [];
foreach ($lists as $total_items) {
$counter += $total_items;
$lists_by_column[$current_column][] = $total_items;
if ($counter > $current_column*$links_per_column) {
$current_column++;
}
}
Results in:
[
[4],
[4,6],
[3,3]
]
But, I want it to look like this:
[
[4,4],
[6],
[3,3]
]
I want to always have the least possible variation in length between the columns.
Other examples of expected results:
[6,4,4,6] => [[6], [4,4], [6]]
[4,4,4,4,6] => [[4,4], [4,4], [6]]
[10,4,4,3,5] => [[10], [4,4], [3,5]]
[2,2,4,6,4,3,3,3] => [[2,2,4], [6,4], [3,3,3]]
Roughly what you need to do is loop over the number of columns within your foreach(). That will distribute them for you.
$numrows = ceil(count($lists) / $columns);
$thisrow = 1;
foreach ($lists as $total_items) {
if($thisrow < $numrows){
for($i = 1; $i <= $columns; $i++){
$lists_by_column[$i][] = $total_items;
}
}else{
//this is the last row
//find out how many columns need to fit.
//1 column is easy, it goes in the first column
//2 columns is when you'll need to skip the middle one
//3 columns is easy because it's full
}
$thisrow++;
}
This will be an even distribution, from left to right. But you actually want a modified even distribution that will look symmetrical to the eye. So within the foreach loop, you'll need to keep track of 1.) if you're on the last row of three, and 2.) if there are 2 remainders, to have it skip col2 and push to col3 instead. You'll need to set that up to be able to play around with it,...but you're just a couple of logic gates away from the land of milk and honey.
So, I ended up using this code:
$lists = [4,4,6,3,3];
$columns = 3;
$total_links = 20;
$items_per_column = ceil($total_links/$columns);
$current_column = 1;
$lists_by_column = [];
for ($i = 0; $i < count($lists); $i++) {
$total = $lists[$i];
$lists_by_column[$current_column][] = $lists[$i];
//Loop until reaching the end of the column
while ($total < $items_per_column && $i+1 < count($lists)) {
if ($total + $lists[$i+1] > $items_per_column) {
break;
}
$i++;
$total += $lists[$i];
$lists_by_column[$current_column][] = $lists[$i];
}
//When exiting the loop the last time we need another break
if (!isset($lists[$i+1])) {break;}
//If the last item goes onto the next column
if (abs($total - $items_per_column) < abs($total + $lists[$i+1] - $items_per_column)) {
$current_column++;
//If the last item goes onto the current column
} else if ($total + $lists[$i+1] > $items_per_column) {
$i++;
$lists_by_column[$current_column][] = $lists[$i];
$current_column++;
}
}
I'm looking to increment a number after a certain letter.
I have a list of own Ids and i would like to increment it without write it manually each time i add a new id.
$ids = array('303.L1', '303.L2', '303.L3', '303.L4');
so i use the END() function to extract the last id from this array.
this is what i've tried but i cannot get a result.
$i = 0;
while($i <= count($ids)){
$i++;
$new_increment_id = 1;
$final_increment = end($last_id) + $new_increment_id;
}
echo $final_increment;
New method, but it is adding me double dot between number and letter.
$i = 0;
while($i <= count($ids)){
$i++;
$chars = preg_split("/[0-9]+/", end($ids));
$nums = preg_split("/[a-zA-Z]+/", end($ids));
$increment = $nums[1] + 1;
$final_increment = $nums[0].$chars[1].$increment;
}
//i will use this id to be inserted to database as id:
echo $final_increment;
Is there another way to increment the last number after L ?
Any help is appreciated.
If you don't want a predefined list, but you want a defined number of ids returned in an $ids variable u can use the following code
<?php
$i = 0;
$number_of_ids = 4;
$id_prefix = "303.L";
$ids = array();
while($i < $number_of_ids){
$ids[] = $id_prefix . (++$i); // adds prefix and number to array ids.
}
var_dump($ids);
// will output '303.L1', '303.L2', '303.L3', '303.L4'
?>
I'm a bit confused because you say "without write it manually". But I think I have a solution:
$ids = array('303.L1', '303.L2', '303.L3', '303.L4');
$i = 0;
while($i <= count($ids)){
++$i;
//Adding a new item to that array
$ids[] = "303.L" . $i;
}
This would increment just that LAST number, starting at zero. If you wanted to continue where you left off, that'd be simple too. Just take $i = 0; and replace with:
//Grab last item in array
$current_index = $ids[count($ids) - 1];
//Separates the string (i.e. '303.L1') into an array of ['303', '1']
$exploded_id = explode('.L', $current_index);
//Then we just grab the second item in the array (index 1)
$i = $exploded_id[1];
hello i'm beginner for programming I've got a homework. googled it but couldnt find anything...
i need to get the total value of numbers from 1 to 10. this need to be done in loop. but couldn't figure which loop should i use. if you can also give me an example code thats would be great.
This is a homework question, I'm not sure why people are just giving you an answer to copy-paste.
Achieving the sum of numbers 1..10 is pretty simple. You will need to initialise an empty int var before your loop, and for each iteration from 0 up to and including 10 you will add your int var to the current iteration.
For example:
sum = 0;
for num in range 1 to 10:
sum = sum + num;
<?php
$start = 0; // set the variable that will hold our total
for($i=1;$i<11;$i++){ // set a loop, read here: http://php.net/manual/en/control-structures.for.php for more info
$start += $i; // add $i to our start value
}
echo $start; // display our final value
I would use a for loop.
$total = 0;
for($i = 1; $i <= 10; $i++){
$total += $i;
}
Using the for loop:
<?php
$sum = 0;
for($i = 1; $i <= 10; $i++){
$sum += $i;
}
Using the foreach loop:
<?php
$sum = 0;
foreach(range(1,10) as $num){
$sum += $num;
}
echo $sum; // prints 55
And disregarding your assignment, here is an easier way:
echo array_sum(range(1,10));