LiTHiUM2525
LiTHiUM2525

Reputation: 307

Read associative array from json received from $_POST

i have a php file receive $_GET informations AND $_POST information, for the $_GET information no problem with that so for the $_POST information i receive this string :

[{"channel":"\/meta\/handshake","id":"10","minimumVersion":"1.0","supportedConnectionTypes":["websocket","long-polling"],"version":"1.0"}]

with [ to start and ] to end.

so how can i read this ? thank you for helping !

Upvotes: 0

Views: 255

Answers (3)

Prasanth Bendra
Prasanth Bendra

Reputation: 32750

Try this :

$str  = '[{"channel":"\/meta\/handshake","id":"10","minimumVersion":"1.0","supportedConnectionTypes":["websocket","long-polling"],"version":"1.0"}]';


echo "<pre>";
$arra   = json_decode($str,true);
print_r($arra);
/*Uncomment this for your out put*/
//echo "Required : ".echo $arra[0]['channel']; 

Output :

Array
(
    [0] => Array
        (
            [channel] => /meta/handshake
            [id] => 10
            [minimumVersion] => 1.0
            [supportedConnectionTypes] => Array
                (
                    [0] => websocket
                    [1] => long-polling
                )

            [version] => 1.0
        )

)

Ref: http://php.net/manual/en/function.json-decode.php

Upvotes: 0

Ric
Ric

Reputation: 8805

You can use the json_decode` function

$channels = json_decode(file_get_contents('php://input')); // parse raw post
print_r($channels) // print structure of channels

Upvotes: 1

Yogesh Suthar
Yogesh Suthar

Reputation: 30488

first decode the json data like this

$arr = json_decode($_POST,true);

then access the data like this

echo $arr[0]['channel']; // output "/meta/handshake"

working example http://codepad.viper-7.com/ZeI9n3

Upvotes: 1

Related Questions