Making JSON PHP

Asked

Viewed 44 times

0

People I’m needing to create a JSON, but I’m not getting it, look at my code below:

$sqlCelula = "SELECT * from tbl_CELULAS WHERE COD_IDENT_IGREJ = 'igj";

$celulaVars = array();
$celulaVars[':igj'] = $suc->getCOD_IDENT_IGREJ();

$celulas = $conexao->fetch($sqlCelula, $celulaVars);

$json = array();

if ($celulas) {

    foreach ($celulas as $celula) {
        $cel = array();

        $cel->TXT_ENDER_CEPXX = $celula->TXT_ENDER_CEPXX;
        $cel->SGL_ENDER_ESTAD = $celula->SGL_ENDER_ESTAD;
        $cel->TXT_ENDER_CIDAD = $celula->TXT_ENDER_CIDAD;
        $cel->TXT_ENDER_BAIRR = $celula->TXT_ENDER_BAIRR;
        $cel->TXT_ENDER_LOGRA = $celula->TXT_ENDER_LOGRA;
        $cel->TXT_ENDER_NUMER = $celula->TXT_ENDER_NUMER;

        $json->CELULAS = $cel;
    }
}

$json_encode = json_encode($json);

In this I search in the bank, with result I do the foreach to walk around the result, and I take one by one of the data I want, in the end I take all this and I put inside the array I created, and then I give json_encode. The result of this is coming out [].

  • 1

    1° you are creating a Cell array every time you run the loop, according to you don’t get value like this, you can use something like $array['keyDoArray'] = value

1 answer

1


Try replacing this snippet of code:

foreach ($celulas as $celula) {
    $cel = array();

    $cel->TXT_ENDER_CEPXX = $celula->TXT_ENDER_CEPXX;
    $cel->SGL_ENDER_ESTAD = $celula->SGL_ENDER_ESTAD;
    $cel->TXT_ENDER_CIDAD = $celula->TXT_ENDER_CIDAD;
    $cel->TXT_ENDER_BAIRR = $celula->TXT_ENDER_BAIRR;
    $cel->TXT_ENDER_LOGRA = $celula->TXT_ENDER_LOGRA;
    $cel->TXT_ENDER_NUMER = $celula->TXT_ENDER_NUMER;

    $json->CELULAS = $cel;
}

For this:

foreach ($celulas as $celula) {
    $cel = array();

    $cel['TXT_ENDER_CEPXX'] = $celula->TXT_ENDER_CEPXX;
    $cel['SGL_ENDER_ESTAD'] = $celula->SGL_ENDER_ESTAD;
    $cel['TXT_ENDER_CIDAD'] = $celula->TXT_ENDER_CIDAD;
    $cel['TXT_ENDER_BAIRR'] = $celula->TXT_ENDER_BAIRR;
    $cel['TXT_ENDER_LOGRA'] = $celula->TXT_ENDER_LOGRA;
    $cel['TXT_ENDER_NUMER'] = $celula->TXT_ENDER_NUMER;

    $json['CELULAS'] = $cel;
}
  • It keeps emptying out the same way.

Browser other questions tagged

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