【问题标题】:Php Array - String Using [closed]Php 数组 - 使用字符串 [关闭]
【发布时间】:2013-08-19 20:13:33
【问题描述】:

我想用数组这个字符串。我怎样才能做到这一点?我分享了我的结果和字符串

我使用了这个代码:

var_dump($result);

结果返回了这个字符串:

"{
    "_": {
        "ID": "tracked"
    },
    "success": true,
    "requestTime": "2013-08-19T13:08:36-07:00",
    "test": "Sometextshere",
    "data": {
        "news": {
            "1": "Hello",
            "13": "New Text",
        },
        "date": "2013-08-19T13:08:36-07:00"
    }
}"

【问题讨论】:

    标签: php arrays string using


    【解决方案1】:

    试试这个:

    <?php
    $result = json_decode($result, true);
    var_dump($result);
    ?>
    

    【讨论】:

    • 当我使用 $result["data"];致命错误:不能使用 stdClass 类型的对象作为数组 in
    【解决方案2】:

    您发布的结果是 JSON 编码的。您首先需要对其进行解码:

    $decoded = json_decode($result);
    

    您可以将结果作为对象或数组返回。默认情况下,它将是一个对象,您可以使用指向语法检索结果。假设您想要测试中的字符串:

    echo $decoded->test;
    

    您还可以使用链式指向来遍历对象属性:

    echo $decoded->data->news;
    

    如果您更喜欢使用数组,您可以在解码中添加一个可选参数。以下是使用数组参数的相同内容:

    $decoded = json_decode($result, true);
    echo $decoded['test'];
    echo $decoded['data']['news'];
    

    希望这会有所帮助!

    【讨论】:

    • 谢谢,但我如何使用随机新闻数组? $decoded['data']['news'][13];是随机的。我怎样才能得到 13?
    • 在这种情况下,13 是字符串而不是整数。不过,您的语法是正确的。 $decoded['data']['news']['13']
    猜你喜欢
    • 2014-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多