无法访问php中的Json解码数组

时间:2014-05-15 06:47:18

标签: php json

我有一个Android应用程序,它将一个json字符串发送到PHP服务器,代码在Android端运行正常,但我想要做的是访问JSon对象php端并将其存储到数据库中,但无论如何都无法访问它。我将在PHP结束时收到的内容返回到android,看看我得到了什么。

Thia是什么应用程序发送到php servere

[
    {
        "title ": "First entry ",
        "address ": "Street 14, Lahore, Pakistan",
        "longitude ": "74.322",
        "latitude ": "31.5365"
    },
    {
        "title ": "Second entry ",
        "address ": "Street 14, Lahore, android ",
        "longitude ": "74.322",
        "latitude ": "31.5365"
    },
    {
        "title ": "Third entry ",
        "address ": "Street 14, Lahore, Lahore ",
        "longitude ": "74.322",
        "latitude ": "31.5365"
    },
    {
        "title ": "Fgghh",
        "address ": "Street 14, Lahore, Pakistan",
        "longitude ": "74.3219",
        "latitude ": "31.5364"
    },
    {
        "title ": "Shsagcg. ",
        "address ": "",
        "longitude ": "74.2695",
        "latitude ": "31.4626"
    },
    {
        "title ": "Ggggfb",
        "address ": "175 Block Q, Lahore, Pakistan",
        "longitude ": "74.2693",
        "latitude ": "31.4626"
    },
    {
        "title ": "",
        "address ": "Street 14, Lahore, Pakistan",
        "longitude ": "74.322",
        "latitude ": "31.5365"
    }
]

Thia是PHP服务器发送回应用程序的原因

Array
(
    [0] => Array
        (
            [title ] => First entry 
            [address ] => Street 14, 
            [longitude ] => 74.322
            [latitude ] => 31.5365
        )

    [1] => Array
        (
            [title ] => Second entry 
            [address ] => Street 14, android 
            [longitude ] => 74.322
            [latitude ] => 31.5365
        )

    [2] => Array
        (
            [title ] => Third entry 
            [address ] => Street 14, 
            [longitude ] => 74.322
            [latitude ] => 31.5365
        )

    [3] => Array
        (
            [title ] => Fgghh
            [address ] => Street 14, 
            [longitude ] => 74.3219
            [latitude ] => 31.5364
        )

    [4] => Array
        (
            [title ] => Shsagcg. 
            [address ] => 
            [longitude ] => 74.2695
            [latitude ] => 31.4626
        )

    [5] => Array
        (
            [title ] => Ggggfb
            [address ] => 175 Block Q, 
            [longitude ] => 74.2693
            [latitude ] => 31.4626
        )

    [6] => Array
        (
            [title ] => 
            [address ] => Street 14, 
            [longitude ] => 74.322
            [latitude ] => 31.5365
        )

)

这是我收到的,我试图用这些命令访问单个值+这里是我的PHP代码。

<?php


$data = file_get_contents("php://input");
$arr = json_decode($data, true);



print_r($arr);// This commands return above string in same format. 

?>

我试过的命令。

$title = $arr[0]['title'];// No success Eror = Undefined index
$title = $arr(0)// of course not success.
$title = $arr[0][1];

有没有办法访问这个数组。

1 个答案:

答案 0 :(得分:4)

您的数组键似乎有一个尾随空格,因此请更改为:

$title = $arr[0]['title '];