0

enter image description hereI want to make my chart data.addColumn ('string', 'hour'); must be a string. In my request it is an int.

$temp_array = array();
while($row = mysqli_fetch_assoc($result))
{
    $temp_array = array(strval($row["hour"]), $row["min"], $row["max"], $row["Avg"]);
}

result now: "["6","30.0","30.0","30.0"]" want only the first one("6") between quotes.

php code

javascript

2
  • See Type Casting. Commented Jul 7, 2022 at 10:12
  • This question is unclear and lacks sufficient debugging details to be useful to future researchers. Adding details as comments under answers is not how question details should be posted. Commented Jul 10, 2022 at 13:14

3 Answers 3

2

Use the strval() to convert integer value to string. For example:

$temp_array[] = array( strval($row["hour"]), strval($row["min"]));
9
  • I am sorry, but seems not to work. I read somewhere that you can't use this in an array?
    – ArjanG
    Commented Jul 7, 2022 at 10:13
  • @ArjanG I believe what you read is that you can't use strval() ON an array. As in, it can't turn an array into a string. It does, however, work INSIDE an array. I tried it myself just now.
    – alex
    Commented Jul 7, 2022 at 10:19
  • Yes, I think your right, but unfortunately this solution doesn't work for me
    – ArjanG
    Commented Jul 7, 2022 at 10:24
  • FYI this does work: 3v4l.org/p7O12
    – Brian
    Commented Jul 7, 2022 at 10:34
  • your example also works on my environment. I'll have to look a bit further as to why it doesn't work in my code.
    – ArjanG
    Commented Jul 7, 2022 at 10:49
1
$temp_array = array();
while($row =mysqli_fetch_assoc($result))
{
    $temp_array[] = array( (string)$row["hour"], $row["min"], $row["max"], $row["Avg"]);
}
4
  • sorry, but seems not to work. Uncaught (in promise) Error: Type mismatch. Value 23 does not match type string in column index 0
    – ArjanG
    Commented Jul 7, 2022 at 10:16
  • @ArjanG you are talking about a JavaScript error message here, while we are discussing PHP code. No one here besides you currently knows what you do with this $temp_array afterwards.
    – C3roe
    Commented Jul 7, 2022 at 10:42
  • When I do an var_dump($temp_array); => string(83) "[["23","20.6","20.6","20.6"],["3","17.0","21.2","19.1"],["6","30.0","30.0","30.0"]]" and all values has quotes. Afterwards I do : ``` $temp_array = json_encode($temp_array); ``` and than in Java: var json_arr = <?php echo json_encode($temp_array); ?>; data.addRows(JSON.parse(json_arr));
    – ArjanG
    Commented Jul 7, 2022 at 11:26
  • FYI, the reason it didn't work before was because I had used 'JSON_NUMERIC_CHECK' => $temp_array = json_encode($temp_array, JSON_NUMERIC_CHECK);
    – ArjanG
    Commented Jul 7, 2022 at 11:40
0

strval($row["hour"]) should do the trick.

6
  • Surely you know by now that the answer is considered low quality unless you have an explanation and/or a reference. :-) Commented Jul 7, 2022 at 11:30
  • @RohitGupta Sure, but to be quite honest, some things are very self-explenatory. If someone asks, "How do I turn a value into a string," why would I explain, "this turns a value into a String"? It's a given.
    – alex
    Commented Jul 7, 2022 at 11:43
  • If you post a self-explanatory answer as a single function solution in 2022, you can be fairly certain that you are answering a duplicate question. Please don't do this. When you answer a page that is later closed as a duplicate, you will prevent the Roomba from doing its important work. Commented Jul 10, 2022 at 13:09
  • @mickmackusa as i am not authorized to mark questions as duplicate, my only alternative option is to ignore said question. i'd rather take the chance i might be helping someone by answering it.
    – alex
    Commented Jul 11, 2022 at 8:06

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.