How to retrieve PHP array for comparison in javascript?

1

I have a code in javascript that sends some elements of the page to another in PHP via ajax and there are some comparisons. Then it returns this array with the print_r () function:

However, I'd like to use return for a second javascript-only comparison, but I do not know how to do it. Is there a way to traverse this return with a for in javascript?

[EDITED]

The object I get on the page I convert to json like this:

var data = JSON.stringify(jsonArr);

And it looks like this:

{"forum":[{"user":"AdministradorGabrielOliveira","tempo":"2017-08-26T16:39:31-03:00","vis":1}]}

Then I send it to the PHP page

$.ajax(
{
    type: 'post',
    url: 'verifica.php',
    data: 'data=' + data,
    success: function(ret) {
        console.log(ret);
}

Some treatments are done and the return with the function print_r () of PHP, thus comes to page where I want to do some operations with javascript:

stdClass Object
(
    [forum] => Array
        (
            [0] => stdClass Object
                (
                    [user] => AdministradorGabrielOliveira
                    [tempo] => 2017-08-26T16:39:31-03:00
                    [vis] => 0
                )

            [1] => stdClass Object
                (
                    [user] => AdministradorGabrielOliveira
                    [tempo] => 2017-08-24T04:57:13-03:01
                    [vis] => 0
                )

        )

)

[RESOLVED]

On the PHP page, I put it like this:

<script>
    var mandar = <?php echo json_encode($jsonTratado); ?>
</script>

So, by going back to the javascript page, I've edited the ajax function to display the send variable from there:

$.ajax({
    type: 'post',
    url: 'verifica.php',
    data: 'data=' + data,
    success: function(ret) {
        console.log(mandar);
    }
);

And for iterating I used forEach as suggested by the colleague in the answer to this question, I just needed to pass the name of the array of objects I was looking for, which in my case is " forum "

$.ajax({
    type: 'post',
    url: 'verifica.php',
    data: 'data=' + data,
    success: function(ret) {
        mandar['forum'].forEach(function(indice){
            console.log(indice.user);
            console.log(indice.tempo);
            console.log(indice.vis);
        });
    }
);
    
asked by anonymous 14.09.2017 / 04:07

1 answer

1

So it will go through the entire data array and each index of the array you can access with the "." (dot) plus the name of the attribute such as "user", see the code below for a better understanding. >

var data = JSON.stringify(jsonArr);
data.forEach(function(element) {
    console.log(element.user);
    console.log(element.tempo);
    console.log(element.vis);
});

With this you can make your comparisons.

    
14.09.2017 / 05:22