How to recover PHP array for javascript comparison?

Asked

Viewed 599 times

1

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

However, I would like to use the return for a second comparison only with javascript, but I don’t know how to do it. There is a way to go through this return with a for javascript?

[EDITED]

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

var data = JSON.stringify(jsonArr);

And it stays that way:

{"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 return with the print_r() function of PHP, so it 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, going back to the javascript page, I edited the ajax function to display the variable send, brought from there:

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

And to iterate I used the foreach as suggested by the colleague in answering this question, I only needed to pass the name of the object array 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);
        });
    }
);
  • Could put the Ajax code to know how does this send.

  • I sent the part of the code I use to send. ;)

  • This using json_encode for return?

  • I am. I have an idea. I will post to receive the tips if this would be the best solution.

  • This returns with utf8 encoding ?

  • I am, @Anthraxisbr

Show 1 more comment

1 answer

1


So it will go through the data array and each input of the Voce array can be accessed with the "."(dot) plus the attribute name as for example "user", see the code below for 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.

  • Opa my friend! I tried here but I returned an error.

  • What error are you presenting?

Browser other questions tagged

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