PHP does not print JSON array

1

Hi guys I need your help in a php module that I'm building, I just want to print a list of cities in JSON but when I do not print anything, I do not know what may be happening: this is my code:

<?php

require "../model/Ciudad.php";

$_POST = $_GET;

function ListaCiudades(){
    $ciudad = new Ciudad();
    $ciudades = $ciudad->Select_Ciudaddes();
    $array_return = array();

    if($ciudades != null){
        foreach ($ciudades as $ciudad) {

            $arr = array("id_ciudad" => $ciudad['PK_ID_CIUDAD'], 
                        "nombre" => $ciudad['NOMBRE'], );
            array_push($array_return, $arr);
        }
    }

    return $array_return;
    // var_dump(json_encode($array_return, JSON_FORCE_OBJECT));
}

/*Identificación del típo de consulta a realizar*/
if(isset($_POST['tipo'])){
    if($_POST['tipo'] == 'consulta'){
        if(isset($_POST['consulta'])){
            switch ($_POST['consulta']) {
                case 'ciudades':
                    $listado = ListaCiudades();
                    echo json_encode($listado); //Aqui es donde no me imprime
                    break;
            }
        }
    }
}



?>

Thanks for your collaboration

    
asked by Felipe Mendieta Perez 20.02.2017 в 20:20
source

1 answer

0

Ready boys, I've got it, this is the code:

<?php

require "../model/Ciudad.php";

$_POST = $_GET;

function ListaCiudades(){
    $ciudad = new Ciudad();
    $ciudades = $ciudad->Select_Ciudaddes();
    $array_return = array();

    if($ciudades != null){
        foreach ($ciudades as $ciudad) {

            $arr = array("id_ciudad" => utf8_encode($ciudad['PK_ID_CIUDAD']), 
                         "nombre" => utf8_encode($ciudad['NOMBRE']));
            array_push($array_return, $arr);
        }
    }

    return $array_return;
    // var_dump(json_encode($array_return, JSON_FORCE_OBJECT));
}

/*Identificación del típo de consulta a realizar*/
if(isset($_POST['tipo'])){
    if($_POST['tipo'] == 'consulta'){
        if(isset($_POST['consulta'])){
            switch ($_POST['consulta']) {
                case 'ciudades':
                    $listado = ListaCiudades();
                    echo json_encode($listado);
                    break;
            }
        }
    }
}
?>

The problem was because of the character set, I solved it by putting utf8_encode when I save each array. Thanks for your help, it was very useful!

    
answered by 20.02.2017 в 20:34