我有一个带地理数据的CSV。我已经创建了一个脚本,将其转换为数组,并将其保存在数据库中。但我想将lat和long保存为小数。但由于一些奇怪的原因,它显示一个值,但当我将它转换为任何类型的浮点数或decimail它显示0?
ini_set('auto_detect_line_endings', TRUE);
$tmpName = $_FILES['fileToUpload']['tmp_name'];
$csvAsArray = array_map('str_getcsv', file($tmpName));
array_shift($csvAsArray); //removes first
array_pop($csvAsArray); //removes last
foreach ($csvAsArray as $k=>$v) {
$municipality = str_replace('"', "", $v[2]);
$city = str_replace('"', "", $v[3]);
$zip = str_replace('"', "", $v[4]);
$lat = $v[5];
$data = [
'country' => $v[0],
'province' => $v[1],
'municipality' => $v[2],
'city' => $v[3],
'nl_fourpp' => $v[4],
'lat' => $v[5],
'lng' => $v[6],
'date_created' => date('Y-m-d H:i:s')
];
var_dump($v[6]); // THIS SHOWS: string(15) "4.91666"
var_dump((float) $v[6]); // THIS SHOWS: float(0)
var_dump(floatval($v[6])); // THIS SHOWS: float(0)
var_dump(number_format($v[6])); // THIS SHOWS: Warning: number_format() expects parameter 1 to be float, string given in
//etc...
//save the fields
...............
}
数据数组看起来像这样(你可以看到它显示了lat和lng .....但是当我将它转换为数字时它只显示0?
Array
(
[country] => NL
[province] => Noord-Holland
[municipality] => Amsterdam
[city] => Amsterdam
[nl_fourpp] => 1000
[lat] => 52.34999
[lng] => 4.91666
[date_created] => 2017-10-21 07:18:21
)
1
当我使用symfonys var dumper转储var时编辑我获得"\x005\x002\x00.\x003\x004\x009\x009\x009\x00"
但是当我变量转储它时我得到"字符串"编号
答案 0 :(得分:0)
改变它
public class SharedPrefManager {
private static SharedPrefManager mInstance;
private static Context mCtx;
private static final String SHARED_PREF_NAME = "mysharedpref12";
private static final String KEY_USERNAME = "username";
private static final String KEY_USER_MAIL = "usermail";
private static final String KEY_USER_ID = "userid";
private static final String KEY_PHONE = "userphone";
private static final String KEY_NAME = "usename";
private static final String KEY_PIN = "pin";
private static final String KEY_SUR = "surname";
private static final String KEY_ADD = "address";
private SharedPrefManager(Context context) {
mCtx = context;
}
public static synchronized SharedPrefManager getInstance(Context context) {
if (mInstance == null) {
mInstance = new SharedPrefManager(context);
}
return mInstance;
}
public boolean userLogin(int id, int pin, String phone, String username, String mail, String name, String surname, String address/**, String catagory*/){
SharedPreferences sharedPreferences = mCtx.getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putInt(KEY_USER_ID, id);
editor.putInt(KEY_PIN, pin);
editor.putString(KEY_PHONE, phone);
editor.putString(KEY_USERNAME, username);
editor.putString(KEY_USER_MAIL, mail);
editor.putString(KEY_NAME, name);
editor.putString(KEY_SUR, surname);
editor.putString(KEY_ADD, address);
//editor.putString(KEY_CATA, catagory);
editor.apply();
return true;
}
public boolean isLoggedIn(){
SharedPreferences sharedPreferences = mCtx.getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE);
if (sharedPreferences.getString(KEY_USERNAME, null) != null){
return true;
}
return false;
}
public boolean logOut(){
SharedPreferences sharedPreferences = mCtx.getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.clear();
editor.apply();
return true;
}
public int getid(){
SharedPreferences sharedPreferences = mCtx.getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE);
return sharedPreferences.getInt(KEY_USER_ID, 0);
//return sharedPreferences.getInt(KEY_USER_ID, Integer.parseInt(null));
}
public int getpin(){
SharedPreferences sharedPreferences = mCtx.getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE);
return sharedPreferences.getInt(KEY_PIN,0);
/*SharedPreferences sharedPreferences = mCtx.getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE);
return sharedPreferences.getInt(KEY_PIN, Integer.parseInt(null));*/
}
public String getUsername(){
SharedPreferences sharedPreferences = mCtx.getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE);
return sharedPreferences.getString(KEY_USERNAME, null);
}
public String getUserEmail(){
SharedPreferences sharedPreferences = mCtx.getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE);
return sharedPreferences.getString(KEY_USER_MAIL, null);
}
public String getUserPhone(){
SharedPreferences sharedPreferences = mCtx.getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE);
return sharedPreferences.getString(KEY_PHONE, null);
}
public String getName() {
SharedPreferences sharedPreferences = mCtx.getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE);
return sharedPreferences.getString(KEY_NAME, null);
}
public String getSurname() {
SharedPreferences sharedPreferences = mCtx.getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE);
return sharedPreferences.getString(KEY_SUR, null);
}
public String getAddress() {
SharedPreferences sharedPreferences = mCtx.getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE);
return sharedPreferences.getString(KEY_ADD, null);
}
}
到
var_dump(number_format($v[6]));
答案 1 :(得分:0)
好吧仍然不知道问题是什么,但我通过做
修复了它 public function FixLatLngStr($str) {
$x = explode("\x00", $str);
array_shift($x); //removes first
array_pop($x); //removes last
return implode("",$x);
}
并返回“正常”浮点值