admin管理员组文章数量:1022981
i have this code :
$wage = array();
foreach ($result3 as $row3) {
$wage[] = '[' . floatval($row3['age']) . ',' . floatval($row3['point']) .']';
}
if ($userid == 0 ) {
$age= "";
} else {
$age = implode(',', $wage).",";
}
echo json_encode("var2"=>$myvar ,"var3"=>$age)); // i didnt write var2 because is same as var3
the above will outputs something like [4,2.3][5,6],[1.7,5],
and in javascript im receiving this value outputed above via ajax which has name var3
.
dataType: 'json' ,
success: function(response){
var age = response['var3'] ;
.....
so the question is how can i parseFloat
this variable age
?
EDIT . if i alert the json like that
alert (response);
i get [object Object]
// make attention to small o and big O
EDIT2 . used consol log
and i get this
{"var2":"[2,5],[3.5,5],[6.5,6.5],[8,7],","var3":"[2,46],[3.5,61],[6.5,70],[8,71],","var4":"[2,32],[3.5,41],[6.5,42],[8,43],","var5":"[46,5],[61,5],[70,6.5],[71,7],"}
this returned values i want to parseFloat
them as you see they are like strings between two quotes
i have this code :
$wage = array();
foreach ($result3 as $row3) {
$wage[] = '[' . floatval($row3['age']) . ',' . floatval($row3['point']) .']';
}
if ($userid == 0 ) {
$age= "";
} else {
$age = implode(',', $wage).",";
}
echo json_encode("var2"=>$myvar ,"var3"=>$age)); // i didnt write var2 because is same as var3
the above will outputs something like [4,2.3][5,6],[1.7,5],
and in javascript im receiving this value outputed above via ajax which has name var3
.
dataType: 'json' ,
success: function(response){
var age = response['var3'] ;
.....
so the question is how can i parseFloat
this variable age
?
EDIT . if i alert the json like that
alert (response);
i get [object Object]
// make attention to small o and big O
EDIT2 . used consol log
and i get this
{"var2":"[2,5],[3.5,5],[6.5,6.5],[8,7],","var3":"[2,46],[3.5,61],[6.5,70],[8,71],","var4":"[2,32],[3.5,41],[6.5,42],[8,43],","var5":"[46,5],[61,5],[70,6.5],[71,7],"}
this returned values i want to parseFloat
them as you see they are like strings between two quotes
-
1
You should use JSON (
json_encode()
) rather manually build JS code. – Crozin Commented Dec 6, 2012 at 21:10 - im using json encode i will edit my post – echo_Me Commented Dec 6, 2012 at 21:11
- Please post the exact JSON output you're getting. – bfavaretto Commented Dec 6, 2012 at 21:18
-
You should be using
json_encode()
for the whole thing, not just the last step. – user149341 Commented Dec 6, 2012 at 21:18 -
alert wont be as helpful with objects as
console.log
will. Trying logging it to the console and hitting F12 (in FF and chrome) to see what log message you get – lbstr Commented Dec 6, 2012 at 21:26
2 Answers
Reset to default 3This is incorrect:
$wage[] = '[' . floatval($row3['age']) . ',' . floatval($row3['point']) .']';
You're building a string, which coincidentally happens to LOOK like a javascript array definition, but it's still just a string. When this gets json_encoded, it'll e out as
"[...,...]"
^-- ^-- string
You need to build NATIVE data structures at all stages while in PHP, e.g.
$wage[] = array(floatval($row3['age']), floatval($row3['point']));
and not this mish-mash of native AND "json-like" strings. json_encode() converts NATIVE datatypes of the equivalent Javascript types. Since your own floatval business is producing a string, you'll get a json-encoded string, not an array.
You are getting this all wrong. You do not need to do this;
foreach ($result3 as $row3) {
$wage[] = '[' . floatval($row3['age']) . ',' . floatval($row3['point']) .']';
}
Perhaps what you want is;
foreach ($result3 as $i => $row3) {
$newRow = $row3;
$newRow['age'] = intval($row3['age']);
$newRow['point'] = floatval($row3['point']);
$result3[$i] = $newRow;
}
And then do this;
// Create JSON data, assuming that $result3 is an array
$jsonData = json_encode($result3);
// This will give you a JSON string, output this and finish to ensure it IS the only thing output
echo $jsonData;
die;
Now in your javascript, open the development console in what ever browser your using and use the following code in javascript
console.log(response)
This will output the whole response
variable to the console and enable you to debug how to get specific data out of the response
var.
Hope that helps.
i have this code :
$wage = array();
foreach ($result3 as $row3) {
$wage[] = '[' . floatval($row3['age']) . ',' . floatval($row3['point']) .']';
}
if ($userid == 0 ) {
$age= "";
} else {
$age = implode(',', $wage).",";
}
echo json_encode("var2"=>$myvar ,"var3"=>$age)); // i didnt write var2 because is same as var3
the above will outputs something like [4,2.3][5,6],[1.7,5],
and in javascript im receiving this value outputed above via ajax which has name var3
.
dataType: 'json' ,
success: function(response){
var age = response['var3'] ;
.....
so the question is how can i parseFloat
this variable age
?
EDIT . if i alert the json like that
alert (response);
i get [object Object]
// make attention to small o and big O
EDIT2 . used consol log
and i get this
{"var2":"[2,5],[3.5,5],[6.5,6.5],[8,7],","var3":"[2,46],[3.5,61],[6.5,70],[8,71],","var4":"[2,32],[3.5,41],[6.5,42],[8,43],","var5":"[46,5],[61,5],[70,6.5],[71,7],"}
this returned values i want to parseFloat
them as you see they are like strings between two quotes
i have this code :
$wage = array();
foreach ($result3 as $row3) {
$wage[] = '[' . floatval($row3['age']) . ',' . floatval($row3['point']) .']';
}
if ($userid == 0 ) {
$age= "";
} else {
$age = implode(',', $wage).",";
}
echo json_encode("var2"=>$myvar ,"var3"=>$age)); // i didnt write var2 because is same as var3
the above will outputs something like [4,2.3][5,6],[1.7,5],
and in javascript im receiving this value outputed above via ajax which has name var3
.
dataType: 'json' ,
success: function(response){
var age = response['var3'] ;
.....
so the question is how can i parseFloat
this variable age
?
EDIT . if i alert the json like that
alert (response);
i get [object Object]
// make attention to small o and big O
EDIT2 . used consol log
and i get this
{"var2":"[2,5],[3.5,5],[6.5,6.5],[8,7],","var3":"[2,46],[3.5,61],[6.5,70],[8,71],","var4":"[2,32],[3.5,41],[6.5,42],[8,43],","var5":"[46,5],[61,5],[70,6.5],[71,7],"}
this returned values i want to parseFloat
them as you see they are like strings between two quotes
-
1
You should use JSON (
json_encode()
) rather manually build JS code. – Crozin Commented Dec 6, 2012 at 21:10 - im using json encode i will edit my post – echo_Me Commented Dec 6, 2012 at 21:11
- Please post the exact JSON output you're getting. – bfavaretto Commented Dec 6, 2012 at 21:18
-
You should be using
json_encode()
for the whole thing, not just the last step. – user149341 Commented Dec 6, 2012 at 21:18 -
alert wont be as helpful with objects as
console.log
will. Trying logging it to the console and hitting F12 (in FF and chrome) to see what log message you get – lbstr Commented Dec 6, 2012 at 21:26
2 Answers
Reset to default 3This is incorrect:
$wage[] = '[' . floatval($row3['age']) . ',' . floatval($row3['point']) .']';
You're building a string, which coincidentally happens to LOOK like a javascript array definition, but it's still just a string. When this gets json_encoded, it'll e out as
"[...,...]"
^-- ^-- string
You need to build NATIVE data structures at all stages while in PHP, e.g.
$wage[] = array(floatval($row3['age']), floatval($row3['point']));
and not this mish-mash of native AND "json-like" strings. json_encode() converts NATIVE datatypes of the equivalent Javascript types. Since your own floatval business is producing a string, you'll get a json-encoded string, not an array.
You are getting this all wrong. You do not need to do this;
foreach ($result3 as $row3) {
$wage[] = '[' . floatval($row3['age']) . ',' . floatval($row3['point']) .']';
}
Perhaps what you want is;
foreach ($result3 as $i => $row3) {
$newRow = $row3;
$newRow['age'] = intval($row3['age']);
$newRow['point'] = floatval($row3['point']);
$result3[$i] = $newRow;
}
And then do this;
// Create JSON data, assuming that $result3 is an array
$jsonData = json_encode($result3);
// This will give you a JSON string, output this and finish to ensure it IS the only thing output
echo $jsonData;
die;
Now in your javascript, open the development console in what ever browser your using and use the following code in javascript
console.log(response)
This will output the whole response
variable to the console and enable you to debug how to get specific data out of the response
var.
Hope that helps.
本文标签: phphow to parseFloat thisStack Overflow
版权声明:本文标题:php - how to parseFloat this? - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745594953a2158109.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论