Receiving Json array in PHP

Asked

Viewed 856 times

2

I have the following code on the application side:

function enviaMensagem() { 
    var send = "http://appchat.host56.com/mensagem.php?jsoncallback=?";
$.post( send, {"table": [{"d":1, "p":1, "m": "1"},{"d":2, "p":2, "m": "2"},{"d":3, "p":3, "m": "3"}]}, function( data ) {}, "json");

}

On the server side I have the following code:

$jsonObj = json_decode($json, true);
$table = $jsonObj->table; 
foreach ( $table as $e ) { 
\\aqui eu gravo o resultado em um BD
}

When I just pass a data string, without having a table, I can manipulate ex: $.post( send, {"d":1, "p":1, "m": "1"}, function( data ) {}, "json");, but passing a table not.

How do I get the data table[0]['d']?


Application side

<script>
    var newUsers = [];

            newUser = {};
            newUser['nome'] = 'alvaro';
            newUser['idade'] = '34';
            newUsers.push(JSON.stringify(newUser));

            newUser1 = {};
            newUser1['nome'] = 'bia';
            newUser1['idade'] = '7';
            newUsers.push(JSON.stringify(newUser1));

            newUser2 = {};
            newUser2['nome'] = 'alice';
            newUser2['idade'] = '2';
            newUsers.push(JSON.stringify(newUser2));

            $.ajax({
                url: "mensagem.php",
                type: "POST",
                data: {
                    'newUsers[]': newUsers
                },
                success: function () {

                },
                error: function () {

                }
            });

<\script>

On the side of the php server I have:

if (isset($_POST['newUsers'])) {
    $newUsers = $_POST['newUsers'];
    var_dump($newUsers);
    foreach ($newUsers as $user) {
        $usr = json_decode($user,false);
        var_dump($usr);
        echo($usr->nome);
    }
}
?>

In the first var_dump of $newusers it has the result: array(3) { [0]=> string(38) "{\"nome\":\"alvaro\",\"idade\":\"34\"}" [1]=> string(34) "{\"nome\":\"bia\",\"idade\":\"7\"}" [2]=> string(36) "{\"nome\":\"alice\",\"idade\":\"2\"}"} and in the second I have: NULLNULLNULL I don’t know how to treat it.

1 answer

2


Your problem is that you are asking for an associative array when you pass true as the second parameter of json_decode.

Use json_decode($json, true) have to use $table = $jsonObj['table'];.

Pass by false making json_decode($json, false) will receive an object and there $table = $jsonObj->table; will already work.

Example: http://ideone.com/Obah0Y

  • When I put the data directly into the php class it works, and when I give the post it doesn’t work

  • @Thiago put here the result of var_dump($jsonObj).

  • it is empty: NULL

  • @Thiago has to put more code to better understand the problem. Where does the $json for example?

  • I just give a $.post( send, {"table": [{"d":1, "p":1, "m": "1"},{"d":2, "p":2, "m": "2"},{"d":3, "p":3, "m": "3"}]}, Function( data ) {}, "json"); on the client side.

  • @Thiago managed to solve the problem?

  • No, in my var_dump I have the result posted string(38) "{"name":"Alvaro","age":"34"}"string(34) "{"name":"Bia","age":"7"}"string(36) "{"name":"Alice","age":"2"}" but I cannot access the data separately

  • @Thiago and did var_dump of what? of $jsonObj? 'cause there’s three here?

  • I ended up changing my $.post method, now I get the Post,

Show 4 more comments

Browser other questions tagged

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