Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

135
Visualizações
PHP String to JS Array

I have two files. The first is my front page that gets information via an ajax function and the second one is a PHP file that gets the data from a database.

The data returned is a string, formatted as an array (example : ['0','0','0','0','0','0','0','0','0','0'] ).

I need the PHP data to be interpreted as a JS array so I can use it easily.

I tried to return a PHP array, but the result in JS is undefined.

Here is my code :

PHP data retriever :

$g = new Game();
$top1 = ($g->getMatchKills($lobby)[0]['p1c1kills']);
$top2 = ($g->getMatchKills($lobby)[0]['p2c1kills']);
$jgl1 = ($g->getMatchKills($lobby)[0]['p1c2kills']);
$jgl2 = ($g->getMatchKills($lobby)[0]['p2c2kills']);
$mid1 = ($g->getMatchKills($lobby)[0]['p1c3kills']);
$mid2 = ($g->getMatchKills($lobby)[0]['p2c3kills']);
$adc1 = ($g->getMatchKills($lobby)[0]['p1c4kills']);
$adc2 = ($g->getMatchKills($lobby)[0]['p2c4kills']);
$sup1 = ($g->getMatchKills($lobby)[0]['p1c5kills']);
$sup2 = ($g->getMatchKills($lobby)[0]['p2c5kills']);

echo "['" . $top1 ."','" .$top2. "','" . $jgl1 . "','" . $jgl2 . "','" . $mid1 . "','" . $mid2 . "','" . $adc1 . "','" . $adc2 . "','" . $sup1 . "','" . $sup2 . "']";

And my JS script is :

function RefreshKDAs() {
    $.ajax({
        url: 'getKills.php?lobby=<?= $lobby ?>',
        success: function (data) {
            console.log(data)
        }
    })
}
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Use json_encode to format a PHP array into JSON:

https://www.php.net/manual/en/function.json-encode.php

about 4 years ago · Juan Pablo Isaza Relatório

0

I solved this using

$data = $g->getMatchKills($lobby)[0];
header("Content-Type: applicaton/json");
echo json_encode($data);

on my data retriever PHP file.

Thanks to Chris G.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda