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

ArjanG
  • 19
  • 5
  • See [Type Casting](https://www.php.net/manual/en/language.types.type-juggling.php#language.types.typecasting). – Álvaro González Jul 07 '22 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. – mickmackusa Jul 10 '22 at 13:14

3 Answers3

2

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

$temp_array[] = array( strval($row["hour"]), strval($row["min"]));
Zain Shabir
  • 126
  • 9
  • I am sorry, but seems not to work. I read somewhere that you can't use this in an array? – ArjanG Jul 07 '22 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 Jul 07 '22 at 10:19
  • Yes, I think your right, but unfortunately this solution doesn't work for me – ArjanG Jul 07 '22 at 10:24
  • FYI this does work: https://3v4l.org/p7O12 – Brian Jul 07 '22 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 Jul 07 '22 at 10:49
  • When I try this: ```$temp_array[] = array( strval($row["hour"]), $row["min"], $row["max"], $row["Avg"]);``` the result is => 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? – ArjanG Jul 07 '22 at 11:34
  • 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 Jul 07 '22 at 11:41
  • @ArjanG share screenshot – Zain Shabir Jul 07 '22 at 11:44
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 07 '22 at 11:44
1
$temp_array = array();
while($row =mysqli_fetch_assoc($result))
{
    $temp_array[] = array( (string)$row["hour"], $row["min"], $row["max"], $row["Avg"]);
}
Dharman
  • 30,962
  • 25
  • 85
  • 135
svgta
  • 343
  • 1
  • 6
  • sorry, but seems not to work. Uncaught (in promise) Error: Type mismatch. Value 23 does not match type string in column index 0 – ArjanG Jul 07 '22 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. – CBroe Jul 07 '22 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 = ; data.addRows(JSON.parse(json_arr));``` – ArjanG Jul 07 '22 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 Jul 07 '22 at 11:40
0

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

alex
  • 576
  • 4
  • 25
  • Surely you know by now that the answer is considered low quality unless you have an explanation and/or a reference. :-) – Rohit Gupta Jul 07 '22 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 Jul 07 '22 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](https://stackoverflow.com/help/roomba) from doing its important work. – mickmackusa Jul 10 '22 at 13:09
  • For your information: [The fundamental goal of closing duplicate questions is to help people find the right answer by getting all of those answers in one place.](https://stackoverflow.com/help/duplicates#:~:text=The%20fundamental%20goal%20of%20closing%20duplicate%20questions%20is%20to%20help%20people%20find%20the%20right%20answer%20by%20getting%20all%20of%20those%20answers%20in%20one%20place.) – mickmackusa Jul 10 '22 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 Jul 11 '22 at 08:06
  • You have unlocked the privilege to flag a question as a duplicate -- that will be infinitely more helpful than posting redundant content. Every time you flag a duplicate instead of answering, you are helping Stack Overflow to be a better platform for sharing knowledge. – mickmackusa Jul 11 '22 at 10:19