0

I am trying to get the input from a 2d php string array and convert the values in arr[i][1] (where i can be treated as a row and 1 as column in matrix) to integer as follows:

JavaScript function:

var _input = <? php echo json_encode($output); ?> ; //2D String
var _sro = []; //string
var _src = []; //string
var _sru = []; //string

var aL = _input.length;
for (var i = 0; i < aL; i++) {
  _sro.push(_input[i][0]); //string
  _src.push(_input[i][1]); //string
  _sru.push(_input[i][2]); //string
}

alert(_src.toString()); //result = 1,1,2,9,6,1,24...
alert(typeof(_src).toString()); //res = string

var _lb = new Array(); //empty
var _dt = new Array(); //empty

for (var i = 0; i < aL; i++) {
  _dt.push(parseInt(_src[i])); //convert to int and push to arr
}

alert(_dt.toString()); //result = NaN,NaN,NaN,NaN.....
alert(typeof(_dt).toString()); //res = string

_lb = _sro;

The problem I am experiencing is that the end output for each value of _dt is NaN which means that it does not get converted appropriately. To resolve this I have tried following:

  1. using parseInt
  2. using Number()
  3. using the "+" operator
  4. using both parseInt and Number()

For example:

for (var i = 0; i < aL; i++) {
  _dt.push(Number(parseInt(_src[i]))); //convert to int and push to arr
}

and all these have failed. I am really lost and I have ran out of ideas after 2 days of googling and maybe someone in their wisdom will be able to help for which thank you in advance.

EDIT:

PHP Function:

the below function generates the PHP array passed through to JS:

//set error reporting
error_reporting(E_ALL & ~E_NOTICE);

//set variables
$result = array();
$output = array();

//read in csv file
foreach(file("1OnScreen.csv") as $key => $str) {
  if ($key == 0)
    continue; // skip first line

  $values = str_getcsv($str, "\t", '', '');
  $result[] = $values;
}
$output = $result;

CSV DATA

The CSV File contains following data and the delimiter is \t:

Owner	Count	Unassigned	Other
AA1	1	0	1
AD	1	0	1
AR	2	0	2
BW	9	0	9
CM	6	0	6
CT	1	0	1
DB	24	0	24
EU	8	0	8
GM	5	0	5
GO	21	0	21
JF	1	0	1
JW	2	0	2
NH	10	0	10
RB	2	0	2
SPC	4	0	4
SP	2	0	2
TC	2	0	2
TG	1	0	1
VS	4	0	4

JSON Output in JS Output of _input as I see it when running alert(_input) i think that the comma on the beginning of the new lines can be impacting the conversion and making it fail but I do not know how to go around that.

AA1,1,0,1
,AD,1,0,1
,AR,2,0,2
,BW,9,0,9
,CM,6,0,6
,CT,1,0,1
,DB,24,0,24
,EU,8,0,8
,GM,5,0,5
,GO,21,0,21
,JF,1,0,1
,JW,2,0,2
,NH,10,0,10
,RB,2,0,2
,SPC,4,0,4
,SP,2,0,2
,TC,2,0,2
,TG,1,0,1
,VS,4,0,4

8
  • Are you sure _src[i] is a properly formatted string? Try for (var i = 0; i < aL; i++) { _dt.push(parseInt(_src[i].toString())); } Commented Dec 10, 2014 at 12:00
  • 1
    Can you please provide the _input array as a javascript variable and not as a php inline script? Commented Dec 10, 2014 at 12:02
  • I also would try for (var i = 0; i < aL; i++) { alert(_src[i].toString()); alert(typeof(_src[i]).toString()); _dt.push(parseInt(_src[i].toString())); } to understand what's going on Commented Dec 10, 2014 at 12:04
  • @dirluca the alerts from the for loop are returning NaN as well. I have double checked the array being passed through and it seems as the array is not being parsed properly. I have included the same data using the var _input = [["AA","1","0","1"],["AD","2","0","7"],["BW","5","0","9"]]; and it now works properly. Commented Dec 10, 2014 at 12:10
  • See this jsfiddle. It works. Are you sure this is the complete code you are using and you didn't post a snippet taking out the error? Commented Dec 10, 2014 at 13:39

1 Answer 1

0

The conversion is failing because of certain special characters in the JSON string passed from the PHP script and "If the first character cannot be converted to a number, parseInt() returns NaN." W3C Source

In order to resolve the failing conversion or rather prevent it happening the use of regular expressions comes in handy (tutorial on regex available here) will lead to replacement of all characters apart from 1-9 (as per this question) such as: /[^1-9 ]/g when trying to convert the string into a number, so that the complete code looks like this:

var _srOwner = <? php echo json_encode($sro_o); ?> ;
var _srCount = <? php echo json_encode($src_o); ?> ;
var _srUnassigned = <? php echo json_encode($sru_o); ?> ;

var _srC = [];
var _srU = [];


var aL = _srCount.length;
var cs = "";
var us = "";
var csi = "";
var usi = "";
var cn = 0;
var un = 0;
for (var i = 0; i < aL; i++) {
  cs = String(_srCount[i]); //string
  us = String(_srUnassigned[i]); //string
  csi = cs.replace(/[^1-9]/g, ""); //string
  usi = us.replace(/[^1-9]/g, ""); //string
  cn = parseInt(csi);
  un = Number(usi);
  _srC.push(cn);
  _srU.push(un);
};

where conversion using both parseInt() and Number() is successful and you may use this question as guidance of which one to chose.

Special thanks for @dirluca for contribution to this question and great pointers.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.