Making PHP JSON

0

I'm trying 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 foreach to walk on the result, and I get one by one of the data that I want, in the end I get all this and put inside the array that I created, and later I give json_encode . The result of this is coming out [] .

    
asked by anonymous 04.02.2016 / 20:27

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;
}
    
04.02.2016 / 20:43