【问题标题】:How to get only a Particular field from an object array in a text file如何从文本文件中的对象数组中仅获取特定字段
【发布时间】:2015-07-15 12:53:52
【问题描述】:

我正在尝试使用 file_get _contents 从文本文件中仅获取 you are doing that too much. try again in 1 minute 字段。请任何人解决这个问题。

{
    "jquery": [
        [
            0,
            1,
            "call",
            [
                "body"
            ]
        ],
        [
            1,
            2,
            "attr",
            "find"
        ],
        [
            2,
            3,
            "call",
            [
                ".status"
            ]
        ],
        [
            3,
            4,
            "attr",
            "hide"
        ],
        [
            4,
            5,
            "call",
            []
        ],
        [
            5,
            6,
            "attr",
            "html"
        ],
        [
            6,
            7,
            "call",
            [
                ""
            ]
        ],
        [
            7,
            8,
            "attr",
            "end"
        ],
        [
            8,
            9,
            "call",
            []
        ],
        [
            1,
            10,
            "attr",
            "find"
        ],
        [
            10,
            11,
            "call",
            [
                ".error.RATELIMIT.field-ratelimit"
            ]
        ],
        [
            11,
            12,
            "attr",
            "show"
        ],
        [
            12,
            13,
            "call",
            []
        ],
        [
            13,
            14,
            "attr",
            "text"
        ],
        [
            14,
            15,
            "call",
            [
                "you are doing that too much. try again in 1 minute."
            ]
        ],
        [
            15,
            16,
            "attr",
            "end"
        ],
        [
            16,
            17,
            "call",
            []
        ]
    ]
}

【问题讨论】:

  • 你试过json_decode吗?
  • 你这样做的时候发生了什么?
  • 您最好自己尝试一下,然后在遇到困难时发布。这不是让其他人为您编写代码的地方。
  • 我收到此错误“未初始化的字符串偏移量
  • 以后@newman 请包含相关的 PHP 代码,以便用户知道您尝试过什么,什么失败了。

标签: php arrays api file-get-contents


【解决方案1】:

您可以使用 cmets 中建议的 json_decode()ass。

如果转换成关联数组。 php.net

<?php

//input your json here
$json = '*** Your json ***'; 

//Decodes a JSON string, When TRUE, it will be converted into associative arrays. 
$array = json_decode($json, true);

//uncomment the next to line sif you want to echo the array
//echo "<pre>";
//print_r($array);

$text = $array['jquery'][14][3][0];
echo $text;

?>

或者如果不转换成关联数组

<?php

//input your json here
$json = '*** Your json ***'; 

//Decodes a JSON string. 
$array = json_decode($json);

//uncomment the next to line sif you want to echo the array
//echo "<pre>";
//print_r($array);

$text = $array->jquery[14][3][0];
echo $text;
?>

【讨论】:

    【解决方案2】:

    你几乎拥有它。访问数组时使用方括号。所以而不是:

    $arr->jquery->{'14'}->{'3'}->{'0'};
    

    ...使用...

    $arr->jquery[14][3][0];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多