How to convert json array to an object in angularjs?

0

I have a mobile app, I have a array to be generated through php what I intend now and in angularjs transform the array into an object because when I want to get for example the id gives undefenid because it is like array and has to be object would like to know how can I transform that array than well of php into an object?

PHP

$result_posts_home = $conexao->prepare("SELECT * FROM posts_home WHERE activo = :activo ORDER BY id DESC ");
$result_posts_home->bindValue(':activo', 1, PDO::PARAM_INT);
$result_posts_home->execute();
$posts_home = $result_posts_home->fetchAll(PDO::FETCH_OBJ);

foreach ($posts_home as $row_posts_home) {

    $result_posts_home_anexos = $conexao->prepare("SELECT * FROM posts_home_anexos WHERE id_mae = :id_mae AND seccao = :seccao ");
    $result_posts_home_anexos->bindParam(':id_mae', $row_posts_home->id, PDO::PARAM_INT);
    $result_posts_home_anexos->bindValue(':seccao', 'thumbnail', PDO::PARAM_STR);
    $result_posts_home_anexos->execute();
    $row_posts_home_anexos = $result_posts_home_anexos->fetch(PDO::FETCH_OBJ);

    // VALIDA SE USER JA TEM LIKE NO POST

    $total_likes = $conexao->prepare("SELECT * FROM likes WHERE id_post = :id_post AND user_id = :user_id ");
    $total_likes->bindValue(':id_post', $row_posts_home->id, PDO::PARAM_INT);
    $total_likes->bindValue(':user_id', $_SESSION['user_id'], PDO::PARAM_INT);
    $total_likes->execute();
    $likes = $total_likes->fetch(PDO::FETCH_ASSOC);


    $noticias[] = array(
        'id'        => $row_posts_home->id,
        'id_anexo'  => $row_posts_home_anexos->id_anexo,
        'tipo'      => $row_posts_home_anexos->tipo,
        'likes'     => $row_posts_home->likes,
    );

}

echo json_encode($noticias);

Controller

.controller('ListaNoticiasHome', function($scope, $http, $stateParams, sessionService, $partilharRecursos) {
    $http.get("https://www.sabeonde.pt/api/api_noticias_home.php").success(function (data) {
        $scope.noticias_home = data;

        $partilharRecursos.set('idNoticia', data.id);
    });
})
    
asked by anonymous 12.10.2015 / 21:21

1 answer

0

You are returning an Array. So it should do like this:

$http.get("https://www.sabeonde.pt/api/api_noticias_home.php").success(function (data) {
    $scope.noticias_home = data[0];

    $partilharRecursos.set('idNoticia', $scope.noticias_home.id);
});

Note that returning an array in your API. It is insecure , so remove it from the server side and return the object directly

Note that I did not do any error handling, or to validate if there is data in the array. Do it according to your need.

Also note that I'm assuming you need only the first element of the array. If this is not the case, I would return json like this:

{"result": [{"id": 1,....}, {"id": 2,....}]}

To format the result in php do the following:

$obj = new stdClass();
$obj->result = $noticias;
echo json_encode($obj);

In angularjs you should iterate these elements and apply your rule as needed:

$http.get("https://www.sabeonde.pt/api/api_noticias_home.php").success(function    (data) {
    $scope.noticias_home = data.result;
    $partilharRecursos.set('idNoticia', data.result[0].id);

    angular.forEach(data.result, function(value, key){
       console.log(key + ': ' + value);
    });        
});
    
12.10.2015 / 22:29