Convert PHP output (mysql query) into a Javascript array

Asked

Viewed 44 times

-1

The result of the query will come out like this (It depends on what was saved in the bank):

$week = 1,2,3

I have the following function:

function verificar(valor) { //Verificar se valor de m está contido em $week
  var m = 1;
  var arrdias = ["<?php echo $week; ?>"];
    function funcD(value) { 
      return value === m;
    }
  var arr = arrdias.some(funcD);
  console.log(arr);
  console.log(arrdias);

... continua se arr for TRUE ...
}

The console.log(arrdias) is coming out like this: ["1,3,5"], but I need him to leave like this: [1, 3, 5] to be able to compare with the var = m, other than the function console.log(Arr) will always give false.

To make it work, I need to convert the PHP result (mysql query) into a javascript array.

I thank anyone who can help me!

1 answer

0


You yourself are putting the quotes there in your code.

Remove and leave the code like this:

function verificar(valor) { //Verificar se valor de m está contido em $week
  var m = 1;
  var arrdias = [<?php echo $week; ?>]; // <<< SEM AS ASPAS AQUI
    function funcD(value) { 
      return value === m;
    }
  var arr = arrdias.some(funcD);
  console.log(arr);
  console.log(arrdias);

... continua se arr for TRUE ...
}
  • 1

    Aff... really... sorry! Thank you very much and may God enlighten you!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.