BcMath Subtracting big numbers + creating a while loop for the difference - php

I got a question how to round numbers of BCMath? And somehow this code doesn't work properly - cause when I remove the text it becomes xxx.00000 . I need really help on this one I got no idea how should it look like to make it working properly.
Code
if (isset($_POST['licz'])) {
$liczba_a='1111111111111111111';
$liczba_b='1111111111111111100';
echo $a = round(bcsub($liczba_a, $liczba_b)).'<br>';
$diffcap = round($a);
//secure 1
$i = 0;
$count = round($diffcap);
$array= array();
while ($i < $count) {
echo 'array '.$b = bcadd($liczba_a, $i).'<br>';
array_push($array, $b);
++$i;
}
var_dump($array);
} else {
echo "Wpisz liczby.";
}
?>
My output
11 - this is the diff number
array 1111111111111111111
array 1111111111111111112
array 1111111111111111113
array 1111111111111111114
array 1111111111111111115
array 1111111111111111116
array 1111111111111111117
array 1111111111111111118
array 1111111111111111119
array 1111111111111111120
array 1111111111111111121
array(11) { [0]=> string(23) "1111111111111111111
" [1]=> string(23) "1111111111111111112
" [2]=> string(23) "1111111111111111113
" [3]=> string(23) "1111111111111111114
" [4]=> string(23) "1111111111111111115
" [5]=> string(23) "1111111111111111116
" [6]=> string(23) "1111111111111111117
" [7]=> string(23) "1111111111111111118
" [8]=> string(23) "1111111111111111119
" [9]=> string(23) "1111111111111111120
" [10]=> string(23) "1111111111111111121
" }

Assign operator has lower precedence than concatination.
echo 'array '.$b = bcadd($liczba_a, $i).'<br>';
^ it would be first operation.
^ it would be second opration.
You should add parentheses
echo 'array '.($b = bcadd($liczba_a, $i)).'<br>';
But better to avoid double purpose operations.
$b = bcadd($liczba_a, $i);
echo 'array '. $b .'<br>';

Related

How do I calculate the sum of neighboring result pairs, and subsequent pairs in succession?

I'm trying to calculate the sum of neighboring two results returned by a MySQL query.
Since I need to calculate the sum of neighboring two results, I need to take the result of the first column and then add it to the second, then I need to take the result of the second column and add it to the third... so on an so forth.
I tried to use the static keyword but it did not work as expected.
$sum = 0;
foreach($test as $key=>$value){
static $q;
$q = $sum+= $value;
}
echo $q;
Schema:
When I use var_dump($test), results are:
array(13) { [0]=> string(5) "21.00" [1]=> string(5) "19.00" [2]=> string(5) "24.00" [3]=> string(6) "277.00" [4]=> string(5) "22.00" [5]=> string(5) "23.00" [6]=> string(5) "21.00" [7]=> string(5) "17.00" [8]=> string(5) "24.00" [9]=> string(5) "21.00" [10]=> string(5) "24.00" [11]=> string(5) "22.00" [12]=> string(5) "22.00" }
Looking at your schema and var_dump result, the solution would be like this:
$nthIndex = count($test) - 1;
$count = count($test[$nthIndex]);
for($i = 0; $i < $count - 1; ++$i){
$sum = $test[$nthIndex][$i] + $test[$nthIndex][$i + 1]; // sum of current and next value
// your code. For example, echo $sum . '<br />';
}
You should consider refactoring the SQL query to get a neat result set.
Update:
As I suspected, the weird array is because OP is doing var_dump($test); inside a loop. The actual output of var_dump($test); looks like this,
array(13) {
[0] => string(5) "21.00"
[1] => string(5) "19.00"
[2] => string(5) "24.00"
[3] => string(6) "277.00"
...
So based on the output, the solution would be like this,
$count = count($test);
for($i = 0; $i < $count - 1; ++$i){
$sum = $test[$i] + $test[$i + 1];
// your code. For example, echo $sum . '<br />';
}

Why is the last element in this array being converted to a reference? (PHP)

I am attempting to trim each string in an array of strings with the code below. However, after executing this function, the last element in the array becomes a reference (note the '&' symbol preceding element #3 in the var_dump). Why does this happen? is it a bug or a PEBKAC?
$x = [" ABC ", "DEF ", " GHI", "JKL "];
// Trim all
foreach ($radio_types as &$r)
{
$r = trim($r);
}
var_dump($x);
// Outputs this:
// array(4) { [0]=> string(3) "ABC" [1]=> string(3) "DEF" [2]=> string(3) "GHI" [3]=> &string(3) "JKL" }

Using OR operator to match the string in php

I am getting past of speech in result. I kept condition to feed noun and adjectives on one array like this:
function printTag($tags) {
$var = array();
foreach($tags as $t) {
echo $t['token'] . "/" . $t['tag'] . " ";
if($t['tag'] == 'NN'|| $t['tag']== 'JJ'){
array_push($var, $t['token']) ;
}
}
return $var;
}
but it does not give correct result:
My out put for echo is this :
The/DT quick/JJ brown/JJ fox/NN
jumped/VBD over/IN the/DT lazy/JJ
dog./NN this/DT is/VBZ really/RB yummy/JJ and/CC excellent/JJ
pizza/NN
I/NN have/VBP seen/VBN have/VBP really/RB in/IN love/NN it/PRP
it/PRP
when I do var_dump($var), it gives:
array(6) {
[0]=>
string(5) "quick"
[1]=>
string(5) "brown"
[2]=>
string(4) "dog."
[3]=>
string(5) "yummy"
[4]=>
string(1) "I"
[5]=>
string(4) "love"
}
why some noun and adjectives skipped?
The reason is that the string you are comparing might have endline or whitespace characters in it.
This can be resolved by using the following:
trim($t['tag']) == 'NN'
This is generally a good idea when comparing strings.

Comparing arrays in PHP - which order - string conversion errors

I var_dumped two arrays, the top on is the array coming in, $new_array, while the other one is a preexisting array $current_array:
// New Array
array(3) {
["Contributor"]=>
array(2) {
[0]=>
string(13) "edit_carousel"
[1]=>
string(13) "read_carousel"
}
["Editor"]=>
array(1) {
[0]=>
string(16) "delete_mini_feed"
}
["Author"]=>
array(3) {
[0]=>
string(11) "edit_blocks"
[1]=>
string(12) "edit_blockss"
[2]=>
string(12) "edit_blockss"
}
}
// Preexisting
array(3) {
["Contributor"]=>
array(2) {
[0]=>
string(13) "edit_carousel"
[1]=>
string(13) "read_carousel"
}
["Editor"]=>
array(4) {
[0]=>
string(16) "delete_mini_feed"
[1]=>
string(15) "edit_mini_feeds"
[2]=>
string(23) "edit_private_mini_feeds"
[3]=>
string(15) "edit_mini_feeds"
}
["Author"]=>
array(3) {
[0]=>
string(11) "edit_blocks"
[1]=>
string(12) "edit_blockss"
[2]=>
string(12) "edit_blockss"
}
}
I am trying to do something like: var_dump(array_intersect_assoc($current_array, $new_array)); to see whats different in the current array as opposed to the new array and generate an array of "differences" keeping the structure intact.
The issue is:
Is the order of arrays to be compared right? compare old to new and get an array of whats different in old. or should it be compare new to old?
Doing this, results in: Array to string conversion notice, but also prints out an array which is below.
I cant tell if these are: "these are whats not in old, but in new" or "the are whats not in new but in old" .... (It should say: these are not whats in old but in new).
array(3) {
["Contributor"]=>
array(2) {
[0]=>
string(13) "edit_carousel"
[1]=>
string(13) "read_carousel"
}
["Editor"]=>
array(4) {
[0]=>
string(16) "delete_mini_feed"
[1]=>
string(15) "edit_mini_feeds"
[2]=>
string(23) "edit_private_mini_feeds"
[3]=>
string(15) "edit_mini_feeds"
}
["Author"]=>
array(3) {
[0]=>
string(11) "edit_blocks"
[1]=>
string(12) "edit_blockss"
[2]=>
string(12) "edit_blockss"
}
}
The php function array_intersect_assoc() should return everything in the first array (aka $current_array) that exists in the second array (aka $new_array).
The issue that you are running into is that array_intersect_assoc() doesn't preform the key comparison recursively. It's only comparing the first level of keys (Contributor, Editor and Author).
Here's more information about the recursive issue. PHP Question: how to array_intersect_assoc() recursively
Your problem is that you are trying to perform array_intersect on a multidimensional array, but the function does string comparison of elements, resulting in array to string conversion error.
As you have the same keys in both arrays, the simplest solution is just to foreach through and to compare subsequent arrays(and you rather need array_diff if you want difference between the elements)
foreach($array_1 as $index => $sub_array) {
$sub_array_2 = $array_2[$index];
$diff = array_diff($sub_array, $sub_array_2);
// do something with diff
}
UPDATE
If You want to get everything that's not in array_1 but in array_2:
$result = [];
# lets find if there's any new elements
$keys_1 = array_keys($array_1);
$keys_2 = array_keys($array_2);
$diff = array_diff($keys_1, $keys_2);
if(!empty($diff)) {
foreach($diff as $key) {
if(isset($array_2[$key])) {
# it's in array_2
$result[$key] = $array_2[$key];
}
}
}
# now get difference between shared elements
$intersection = array_intersect($keys_1, $keys_2);
foreach($intersection as $key) {
$element_1 = $array_1[$key];
$element_2 = $array_2[$key];
$diff = array_diff($element_1, $element_2);
if(sizeof($diff)) {
if(!isset($result[$key]) ||!is_array($result[$key]) ) {
$result[$key] = array($diff);
} else {
$result[$key][] = $diff;
}
}
}

sort an array by an index preset

i have this array
$immagini = array('1.jpg','2.jpg','3.jpg','4.jpg');
if i make var_dump($immagini) return this
array(4) {
[0]=> string(5) "1.jpg"
[1]=> string(5) "2.jpg"
[2]=> string(5) "3.jpg"
[3]=> string(5) "4.jpg"
}
now how do I order them in another way, i like start from index [2]....so i want this result
array(4) {
[2]=> string(5) "3.jpg"
[3]=> string(5) "4.jpg"
[0]=> string(5) "1.jpg"
[1]=> string(5) "2.jpg"
}
I would like from an index and return the complete list
You can't use var_dump to accomplish that. You'd need your own loop to print from the custom starting index. Start from the starting index and increment your loop iterator by one at each iteration until you have looped length of array times. When you want to access an array element, use loopIterator mod length of array
You could sort them with a comparison function that moves all index < 2 to the end of the list. For example, here would be such a comparison:
function cmp($a,$b) {
$a = ($a < 2) ? $a + 1000 : $a;
$b = ($b < 2) ? $b + 1000 : $b;
return $a - $b;
}
Called like this:
$immagini = array('1.jpg','2.jpg','3.jpg','4.jpg');
uksort($immagini, 'cmp');
var_dump($immagini);
Gives the following output:
array(4) {
[2]=>
string(5) "3.jpg"
[3]=>
string(5) "4.jpg"
[0]=>
string(5) "1.jpg"
[1]=>
string(5) "2.jpg"
}
Demo: http://ideone.com/XeAkQL

Categories