I'm having to pass 3 variables (int) within a single numeric string called $id. To do this I'm creating $id using padding which I can then explode to get the variables. It has to be numeric otherwise I'd use underscores between the variables. I'm using eleven zeros as padding as I know the variables won't have that many zeros. So currently if I have:
$int_one = 1;
$int_two = 2;
$int_three = 3;
That would be:
$id = "1000000000002000000000003";
To create the new Id I use:
$id = $int_one . "00000000000" . $int_two . "00000000000" . $int_three;
And to separate the Id I use:
$int_one = 0;
$int_two = 0;
$int_three = 0;
if (strpos($id,"00000000000") !== false) {
$id = strrev($id); // Reversed so 0's in int's don't get counted
$id = explode("00000000000", $id);
// Set numbers back the right way
$int_one = strrev($id[2]);
$int_two = strrev($id[1]);
$int_three = strrev($id[0]);
}
This runs into problems when an individual variables is 0. Is there a way to overcome this or does it need a major rethink?
EDIT: $id is supposed to be a numeric string not int
Needs to handle int variables between 0 - 2147483647
You can just use some string magic to assure that no number has more than one zero in a row, and delimit the values using '00'. This generates a numeric string that can be uniquely decoded no matter the size or composition of the ints.
$a = 100;
$b = 0;
$c = 120;
// Encode;
$id = str_replace('0', '01', $a).'00'
.str_replace('0', '01', $b).'00'
.str_replace('0', '01', $c);
// $id = "101010001001201"
// Decode;
$tmp = split('00', $id);
$a2 = intval(str_replace('01', '0', $tmp[0]));
$b2 = intval(str_replace('01', '0', $tmp[1]));
$c2 = intval(str_replace('01', '0', $tmp[2]));
// $a2 = 100, $b2 = 0, $c2 = 120
Is there a way to overcome this or does it need a major rethink?
Yes, you'll need to rethink that. Why do you need to do it that way? Simply create a function with three parameters and pass the three ints in:
function foo($int1, $int2, $int3) {
}
Your example uses strings, not ints by the way, so you aren't even following your own requirements.
You could try this method:
$int_one = 1;
$int_two = 2;
$int_three = 3;
$id = $int_one * 1000000000000 + $int_two * 1000000 + $int_three;
// This will create a value of 1000002000003
To reverse the process:
// Get the modulo of $id / 1000000 --> 3
$int_three = $id % 1000000;
// Recalculate the base id - if you would like to retain the original id, first duplicate variable
// This would make $id = 1000002;
$id = ($id - $int_three) / 1000000;
// Again, get modulo --> 2
$int_two = $id % 1000000;
// Recalculate base id
$id = ($id - $int_two) / 1000000;
// Your first integer is the result of this division.
$int_one = $id;
Related
Can anyone provide an alternative answer to my variable summations without it being a floating point decimal result with the possibility of displaying the final result as in an interger value. Whole numbers to be exact.
With every variable passing results in decimal notation, the final value is larger than the actual pen to paper I get using calculator.
This yeld s echo $terresulting in float value 43402777.777778. It should be more in the lines of 42,849,000 instead of the former, the latter value is my pen and paper. The problem is outputting final value to be non scientific notation. I need in straight interger value.
$tan = 100000;
$g = 30;
$epp = 12;
$days = 365;
$bpe = 4;
$pp = 300;
$apg = $tan / $g;
$epg = $apg / $bpe;
$ppg = $epg / $epp;
$ypr = $ppg * $pp;
$tep = $ppg * $g;
$ter = $ypr * $tep;
echo $ter;
As is typical in computer science, floating-point arithmetic comes with its own set of problems.
Libraries are generally available for more precision. PHP offers the BC Math functions.
Your example can be altered to use this...
$tan = 100000;
$g = 30;
$epp = 12;
$days = 365;
$bpe = 4;
$pp = 300;
$apg = bcdiv($tan, $g);
$epg = bcdiv($apg, $bpe);
$ppg = bcdiv($epg, $epp);
$ypr = bcmul($ppg, $pp);
$tep = bcmul($ppg, $g);
$ter = bcmul($ypr, $tep);
echo '$ter: ', $ter, PHP_EOL; // 42849000
echo 'Formatted: ', number_format($ter), PHP_EOL; // 42,849,000
Demo ~ https://3v4l.org/vlRaS
I want to merge cells dynamically based on count using PHPEXCEl.
For example:
if $count = 2;
I want to merge two cells as given below,
$objPHPExcel->getActiveSheet()->mergeCells('A1:B1');
similarly, if $count = 4;
$objPHPExcel->getActiveSheet()->mergeCells('C1:F1');
similarly, if $count = 5;
$objPHPExcel->getActiveSheet()->mergeCells('G1:K1');
I want to get this logic in a loop.
I tried the below logic, which doesn't work
$count = ew_Execute("SELECT COUNT(*) FROM ems_defects_codes WHERE DEF_CODE = '$def_code'");
$start_letter = A;
$rowno = 1;
for ($i = 0; $i < $count ; $i++) {
$objPHPExcel->getActiveSheet()->mergeCells($start_letter.$rowno.':'.$i.$rowno);
}
Any help will be much appreciated.Thanks..!!
You need to get column range string value for the inputs - start_letter, row_number and count. Once the column range is available, same can be used in the PHPExcel mergeCells function. Here is example code to get column range:
function getColRange($start_letter, $row_number, $count) {
$alphabets = range('A', 'Z');
$start_idx = array_search(
$start_letter,
$alphabets
);
return sprintf(
"%s%s:%s%s",
$start_letter,
$row_number,
$alphabets[$start_idx + $count],
$row_number
);
}
print getColRange('A', 1, 2) . PHP_EOL;
print getColRange('C', 1, 4) . PHP_EOL;
print getColRange('G', 1, 4) . PHP_EOL;
Output
A1:C1
C1:G1
G1:K1
Further you can use this new function with your code to do actual merge. You can choose to call this function or in a loop.
$sheet = $objPHPExcel->getActiveSheet();
$sheet->mergeCells(
getColRange(
$start_letter,
$row_number,
$count
)
);
The problem is that your $i inside of your loop is always going to be an integer; you need to convert that integer to the corresponding index of the alphabet, by creating an alphabetic array. This can be done with a simple range('A', 'Z').
You also need to wrap the A in $start_letter in apostrophes (as 'A'), and now that the range has been created, you can simply use the index of the alphabet for that:$start_letter = 0 (later becoming 'A' with $alphabet[$start_letter]).
Then you'll need to add the starting letter to the count for in order to get the ending cell in mergeCells(). Your starting cell now becomes $alphabet[$start_letter] . $rowno, and your ending cell now becomes ($alphabet[$start_letter] + $alphabet[$i]) . $rowno.
This can be seen in the following:
$count = ew_Execute("SELECT COUNT(*) FROM ems_defects_codes WHERE DEF_CODE = '$def_code'");
$alphabet = range('A', 'Z');
$start_letter = 0;
$rowno = 1;
for ($i = 0; $i < $count; $i++) {
$objPHPExcel->getActiveSheet()->mergeCells($alphabet[$start_letter] . $rowno . ':' . ($alphabet[$start_letter] + $alphabet[$i]) . $rowno);
}
I'm completely stumped on how to accomplish my task programmatically.
I have a dynamically changing alpha string variable named $string. I have corresponding values set to each letter in the alphabet. I need to be able to automatically calculate the value of $total_string_length based on the letters within the $string variable.
$string = "jack"
$A_length = 1000;
$B_length = 500;
$C_length = 200;
$D_length = 1000;
$E_length = 1400;
$F_length = 100;
$G_length = 5000;
$H_length = 2000;
$I_length = 600;
$J_length = 8000;
$K_length = 8000;
etc...
$total_string_length = $J_length + $A_length + $C_length + $K_length
How do I easily substitute the corresponding alpha letter variable values to find the $total_string_length ?
Any help is much appreciated.
PHP can use dynamic variable names.
$string = "jack";
$A_length = 1000;
$B_length = 500;
$C_length = 200;
$D_length = 1000;
$E_length = 1400;
$F_length = 100;
$G_length = 5000;
$H_length = 2000;
$I_length = 600;
$J_length = 8000;
$K_length = 8000;
$output = 0;
for($i = 0; $i < strlen($string); $i++) {
$varName = strtoupper($string[$i]) . "_length";
$output += $$varName; // notice the double `$` charaters
}
echo $output;
In the above code snippet, the value of the $varName variable is the name of the variable we want to get the number from. If $varName is A_length then $$varName is equivalent to $A_length.
However, it's better to store your number in an array instead of a list of separated variables.
Not much into PHP, But in Java we can do something as below:
Create an integer array of size 26 (number of letters in the alphabet) and load the length value of each variable's first character based on its index i.e.,
intArr[(int)"A_length".charAt(0) - 65] = 1000;
Iterate through the characters of the string and get the value as stated below:
for ( char ch : string.tocharArray() ) {
sum += intArr[(int)ch - 65 ];
}
I want to check value with in range or not suppose if I have range D1 to D40 and if I enter D20 then it returns value with in range.
I check several solution but this are for only integer not for both string and integer.
EDIT
Range will be dynamic like AA20 to AA30 or like AC10D to AC30D
You can write something simpler like this...
$arr = range(1,40); //<--- Creating a range of 1 to 40 elements..
array_walk($arr,function (&$v){ $v = 'D'.$v;}); //<--- Concatenating D to all the elements..
echo in_array('D20',$arr) ? 'Found' : 'Not Found'; //<-- The search part.
Demonstration
First, you should remove the letter D from your string variable, like this:
// This is your first variable:
$rang1="D5";
// This is your second rang variable:
$rang2="D20";
$rang1=str_replace("D","",$rang1);
$rang2=str_replace("D","",$rang2);
$rang=$rang2-$rang1;
echo $rang;
Or if your variable looks like this:
$rang="D5 TO D20";
you can use the following:
$rang="D5 TO D20";
$rang=explode(" TO ",$rang);
$rang1=rang[0];
$rang2=rang[1];
$rang1=str_replace("D","",$rang1);
$rang2=str_replace("D","",$rang2);
$rang=$rang2-$rang1;
echo $rang;
// 1. build up array of valid entries
$prefix = "D";
$rangeArray = array();
for($i = 1; $i <= 40; $i++) {
$rangeArray[] = $prefix . $i;
}
...
// 2. check against that array:
$inRange = in_array($needle, $rangeArray); // boolean
To get the position in the range:
$pos = array_search($needle, $rangeArray); // integer or false if not found
Where $needle would be your input value.
The following code will work with ranges with different letter in the beginning like A10 to B30 (assuming A20 is in that range, but A40 is not):
$min = "A10";
$max = "B30";
$test = "A20";
$min_ascii = chr($min[0]);
$max_ascii = chr($max[0]);
$test_ascii = chr($max[0]);
$min_number = substr($min, 1);
$max_number = substr($max, 1);
$test_number = substr($test, 1);
if ($min_ascii <= $test_ascii and $test_ascii <= $max_ascii
and $min_number <= $test_number and $test_number <= $max_number)
{
echo "$test is in the range from $min to $max";
}
Well, ths is a really newbie question, but couldn't find the proper keywords to get the answer.
I have a vector variable, $p.
I want to achieve this:
$n = 5;
$prev = $p[$n--];
$actual = $p[$n];
$next = $p[$n++];
The values i want should be:
$prev = 4;
$actual = 5;
$next = 6;
Instead of that, i get:
$prev = 5;
$actual = 4;
$next = 4;
I know i'm missing something, but i couldn't figure it out.
Thanks in advance
Just do the math yourself:
$prev = $p[$n - 1];
$actual = $p[$n];
$next = $p[$n + 1];
If you must use increment / decrement operators (for some strange reason), you can use:
$prev = $p[--$n];
$actual = $p[++$n];
$next = $p[++$n];
Note that your original code was failing, as jeremy points out, because increment and decrement operators modify the original value of the variable.
At the first search:
$prev = $p[$n--]
You are changing the value of n for the next operations, decreasing it by one. In
$next = $p[$n++];
You are increasing it again so n value is the same as the beginning. You should do what nickb told.
You are incrementing the same variable inline so:
$n doesn't stay as 5 when you do $n++ or $n--, you reassign the value to $n
$n-- = 4, so now $n is 4, not 5 so $n++ is 5, not 6