number_format() fails to convert scientific notation - php

I have some float values being looped through an array that are outputting in scientific notation.
I need to convert the scientific notation to a regular flat float long notation value.
I tried number_format(), but it still outputs in scientific notation until the very last cycle of my loop?
<?php
$parray = array(array('BTC-EXP','1',0.000097,1),array('BTC-VOX',7,number_format(0.000075,8),7));
for ($x = 0; $x <= 1; $x++) {
var_dump($parray);
foreach($parray as $pairp){
echo "\nDebug info! Limit\n";
echo $pairp[2];
echo "\n";
print_r(number_format($pairp[2],8));
}
}
?>
output:
Debug info! midpoint
array(2) {
[0]=>
array(4) {
[0]=>
string(7) "BTC-EXP"
[1]=>
string(1) "1"
[2]=>
float(9.7E-5)
[3]=>
int(1)
}
[1]=>
array(4) {
[0]=>
string(7) "BTC-VOX"
[1]=>
int(7)
[2]=>
string(10) "0.00007500"
[3]=>
int(7)
}
}
Debug info! Limit
9.7E-5
0.00009700
Debug info! Limit
0.00007500
0.00007500
Debug info! midpoint
array(2) {
[0]=>
array(4) {
[0]=>
string(7) "BTC-EXP"
[1]=>
string(1) "1"
[2]=>
float(9.7E-5)
[3]=>
int(1)
}
[1]=>
array(4) {
[0]=>
string(7) "BTC-VOX"
[1]=>
int(7)
[2]=>
string(10) "0.00007500"
[3]=>
int(7)
}
}
Debug info! Limit
9.7E-5
0.00009700
Debug info! Limit
0.00007500
0.00007500
How do I convert my float values to long notation through an entire cycle (deny short notation)?

In your $parray, you are storing one number as a float (0.000097) and in the other you're storing the result of number_format(0.000075,8), which is a string.
It's not surprising then, that when you call
echo $pairp[2]; on 0.000097, that you see 9.7E-5, but when you echo it for number_format(0.000075,8) you see a string representation (0.00007500).
Let's do the same for print_r(number_format($pairp[2],8));. When it's called for float (0.000097), it formats it as a string and you see 0.00009700. When it's called for "0.00007500", this string is converted to a float 0.000075, which is then converted back to a string "0.00007500".

Related

PHP-SQL-Parser Why this lib parsed 'SIGNED' as a colref instead of reserved word?

Thanks for your help. So the question I'm asking is - Why https://github.com/greenlion/PHP-SQL-Parser parsed 'SIGNED' as a colref? -. Is that a bug or there is a reason for that?
I'm parsing an expression like "WHERE ( CAST(table1.BIGINTField AS SIGNED) - CAST(table2.BIGINTField AS SIGNED) ) <= 11". The reason for the cast is that mysql fires a warning when trying to do so without the cast. I was reading the docs and I found this way to fix that, casting the bigint fields to signed. I'm using the version 4.1.1 of PHP-SQL-PARSER, I got problems with the last version.
This is the result I have got so far:
["WHERE"]=> array(3) {
[0]=> array(4) {
["expr_type"]=> string(8) "function"
["base_expr"]=> string(4) "CAST"
["sub_tree"]=> array(1) {
[0]=> array(5) {
["expr_type"]=> string(10) "expression"
["base_expr"]=> string(51) "`table1`.`bigintfield` AS SIGNED"
["sub_tree"]=> array(3) {
[0]=> array(5) {
["expr_type"]=> string(6) "colref"
["base_expr"]=> string(41) "`table1`.`bigintfield`"
["no_quotes"]=> array(2) {
["delim"]=> string(1) "."
["parts"]=> array(2) {
[0]=> string(26) "table1"
[1]=> string(10) "bigintfield"
}
}
["sub_tree"]=> bool(false)
["position"]=> int(11)
}
[1]=> array(4) {
["expr_type"]=> string(8) "reserved"
["base_expr"]=> string(2) "AS"
["sub_tree"]=> bool(false)
["position"]=> int(53)
}
[2]=> array(5) {
["expr_type"]=> string(6) "colref"
["base_expr"]=> string(6) "SIGNED"
["no_quotes"]=> array(2) {
["delim"]=> bool(false)
["parts"]=> array(1) {
[0]=> string(6) "SIGNED"
}
}
["sub_tree"]=> bool(false)
["position"]=> int(56)
}
}
["alias"]=> bool(false)
["position"]=> int(11)
}
}
["position"]=> int(6)
} ...
Signed is a reserved word, I don't know why the sql parser say that it isn't.
Even though, Is there any different way to fix the problem facing arithmetical operations with bigints? I hope I'm not saying any foolishness haha.
Update: I almost forgot, I "fixed" it adding an if statement, performing the operation just when the value of the table1 is bigger than the value of the table2 but still.
Thanks for your help!
Sebastian C.

Getting value with key from multidimensional array not returning expected result

I'm currently trying to loop over an array and only get specific values with key 1, but for some reason it's giving me another array instead of the string value I'm trying to retrieve.
//using firstElement variable to skip first row of excel sheet
$firstElement = true;
foreach ($arrayData as $excelData){
if($firstElement){
$firstElement = false;
}
else{
$latinName = array_column($excelData, 1);
var_dump($latinName);
if(strtolower($latinName) == strtolower($target3[0])){
echo('target is already in database' . PHP_EOL);
}
else{
echo($latinName . 'should be written to database' . PHP_EOL);
}
}
}
this is the current for each loop I'm using. The variable arrayData is an array with every result I'm getting from an excel sheet. My goal is to get the specific field latinName from each row.
result from the variable dump is a list of arrays:
[121]=>
array(7) {
[0]=>
string(14) "Knolboterbloem"
[1]=>
string(19) "Ranunculus bulbosus"
[2]=>
string(7) "Plantae"
[3]=>
string(13) "Magnoliophyta"
[4]=>
string(12) "Angiospermae"
[5]=>
string(14) "Basal eudicots"
[6]=>
string(12) "Ranunculales"
}
My goal is to get every value with key 1 from those arrays and assign them to variable latinName using this code (edited this code to test Mark Bakers answer):
$latinName = array_column($excelData, 1);
But whenever I print out the value from latinName I get another array instead of the String value:
array(17) {
[0]=>
NULL
[1]=>
string(10) "Poa annua "
[2]=>
string(22) "Alopecurus myosuroides"
[3]=>
string(22) "Echinochloa crus-galli"
[4]=>
string(17) "Apera spica-venti"
[5]=>
string(14) "Milium vernale"
[6]=>
string(14) "Lolium perenne"
[7]=>
string(20) "Agrostis stolonifera"
[8]=>
string(15) "Holcus lanatus "
[9]=>
string(14) "Avena sterilis"
[10]=>
string(19) "Digitaria ischaemum"
[11]=>
string(15) "Setaria viridis"
[12]=>
string(20) "Setaria verticillata"
[13]=>
string(18) "Cyperus esculentus"
[14]=>
string(16) "Eragrostis minor"
[15]=>
string(13) "Holcus mollis"
[16]=>
string(17) "Scirpus maritimus"
}
I don't use PHP that often, but why is my variable latinName getting turned into an array?
Any help would be greatly appreciated and sorry for the messy post.

array has same key twice (php)

im reading in some data and want to sort it into an array.
To be more specific, im reading in an obj model and i want to sort all edges into an array. Each edge element then should exist of an array of all faces which use this edge. So after sorting, each edge should have 2 entries existing of the two faces which use them. But after the actual sorting most edges occur twice with the same key and a single face as an entry. Here in some more details what i mean :
First Face i read in is
array(4) {
[0]=>
string(1) "f"
[1]=>
string(1) "1"
[2]=>
string(1) "2"
[3]=>
string(2) "3
which sorts the edges 1_2 , 2_3 and 1_3 into this array (value is the face which uses this edge, face number 0 )
array(3) {
["1_2"]=>
array(1) {
[0]=>
int(0)
}
["2_3"]=>
array(1) {
[0]=>
int(0)
}
["1_3"]=>
array(1) {
[0]=>
int(0)
}
When the second face is read
array(4) {
[0]=>
string(1) "f"
[1]=>
string(1) "1"
[2]=>
string(1) "3"
[3]=>
string(2) "4"
the resulting edge array looks like this :
array(6) {
["1_2"]=>
array(1) {
[0]=>
int(0)
}
["2_3"]=>
array(1) {
[0]=>
int(0)
}
["1_3"]=>
array(1) {
[0]=>
int(0)
}
["1_3"]=>
array(1) {
[0]=>
int(1)
}
["3_4"]=>
array(1) {
[0]=>
int(1)
}
["1_4"]=>
array(1) {
[0]=>
int(1)
}
}
the edge 1_3 is used by both faces, but how the heck does the key "1_3" appear twice in a single array with different values?! i expected it to look like this
["1_3"]=>
array(2) {
[0]=>
int(0)
[0]=>
int(1)
}
i do not have a clue why or how this happens, because as far as i know keys should be unique. when giving out the element "1_3" i then only recieve the latest entry ( int(1) ).
full sorting code looks like this
if ($line_temp[0]=="f"){
for ($j=0;$j<3;$j++){
$line_temp2 = explode("//",$line_temp[$j+1]);
$data["f"][$face_number][$j] = $line_temp2[0];
}
if ($data["f"][$face_number][0]<$data["f"][$face_number][1]){
$data["e"][$data["f"][$face_number][0]."_".$data["f"][$face_number][1]][]=$face_number;
} else {
$data["e"][$data["f"][$face_number][1]."_".$data["f"][$face_number][0]][]=$face_number;
}
if ($data["f"][$face_number][1]<$data["f"][$face_number][2]){
$data["e"][$data["f"][$face_number][1]."_".$data["f"][$face_number][2]][]=$face_number;
} else {
$data["e"][$data["f"][$face_number][2]."_".$data["f"][$face_number][1]][]=$face_number;
}
if ($data["f"][$face_number][0]<$data["f"][$face_number][2]){
$data["e"][$data["f"][$face_number][0]."_".$data["f"][$face_number][2]][]=$face_number;
} else {
$data["e"][$data["f"][$face_number][2]."_".$data["f"][$face_number][0]][]=$face_number;
}
var_dump($data["e"]);
var_dump($data["e"]["1_3"]);
$face_number++;
}

Php getting all values from the string

By doing ajax I am getting one result in php like this
source_lang_pair[]=EN-NO&source_lang_pair[]=NE-NO&source_lang_pair[]=HI-NO&source_lang_pair[]=OR-NO&source_lang_pair[]=JA-NO&resource_search_name=aa
I want to extract all the values of source_lang_pair and resource_search_name value together.
I have tried parse_str but its not working. So can someone tell me how to extract those values from the string?
parse_str works great like this:
<?php
$str = "source_lang_pair[]=EN-NO&source_lang_pair[]=NE-NO&source_lang_pair[]=HI-NO&source_lang_pair[]=OR-NO&source_lang_pair[]=JA-NO&resource_search_name=aa";
parse_str($str);
echo $resource_search_name;
echo "<br/>";
var_dump ($source_lang_pair);
Output:
aa
array(5) {
[0]=>
string(5) "EN-NO"
[1]=>
string(5) "NE-NO"
[2]=>
string(5) "HI-NO"
[3]=>
string(5) "OR-NO"
[4]=>
string(5) "JA-NO"
}
or you can use like this for uncertainty:
<?php
$str = "source_lang_pair[]=EN-NO&source_lang_pair[]=NE-NO&source_lang_pair[]=HI-NO&source_lang_pair[]=OR-NO&source_lang_pair[]=JA-NO&resource_search_name=aa";
parse_str($str, $output_array);
var_dump ($output_array);
This time your output will be:
array(2) {
["source_lang_pair"]=>
array(5) {
[0]=>
string(5) "EN-NO"
[1]=>
string(5) "NE-NO"
[2]=>
string(5) "HI-NO"
[3]=>
string(5) "OR-NO"
[4]=>
string(5) "JA-NO"
}
["resource_search_name"]=>
string(2) "aa"
}

Coordinates from text to array [preg_match]

Maybe someone can help me get coordinates from text to array?
I'm trying use preg_match but no success.
I can remove geometry":{"paths":[[[**" and **]]}}]})
then use some times 'explode' and get coo, but I don't like this idea
Coordinates:
"geometry":{"paths":
[[[485749.91999999998,6108180.6500000004],
[485722.35999999999,6108206],
[485691.14000000001,6108234.3099999996],
[485400.84999999998,6108513.1500000004],
[485368.60999999999,6108545.46],
[485301.53999999998,6108613.1900000004],
[484054.82000000001,6109868.9100000001],
[484051.17566666665,6109872.6840000004]]]}}]});
To figure out my approach in the comment above. Convert the string to valid JSON. Afterwards you can decode the string using json_decode():
$jsonArr = json_decode('{' . substr($string, 0, -4), true);
$coordinates= $jsonArr['geometry']['paths'][0]);
var_dump($coordinates):
So you end up with an array of coordinate pairs:
array(8) {
[0]=>
array(2) {
[0]=>
float(485749.92)
[1]=>
float(6108180.65)
}
[1]=>
array(2) {
[0]=>
float(485722.36)
[1]=>
int(6108206)
}
[2]=>
array(2) {
[0]=>
float(485691.14)
[1]=>
float(6108234.31)
}
[3]=>
array(2) {
[0]=>
float(485400.85)
[1]=>
float(6108513.15)
}
[4]=>
array(2) {
[0]=>
float(485368.61)
[1]=>
float(6108545.46)
}
[5]=>
array(2) {
[0]=>
float(485301.54)
[1]=>
float(6108613.19)
}
[6]=>
array(2) {
[0]=>
float(484054.82)
[1]=>
float(6109868.91)
}
[7]=>
array(2) {
[0]=>
float(484051.175667)
[1]=>
float(6109872.684)
}
}
This is invalid Javascript object notation. Running it through JSONLint shows that it has an invalid array length. You might want to provide more context for your question.
Assuming you have something similar to the above but with valid JSON, you can use the json_decode method to convert a valid JSON string into PHP arrays and objects.

Categories