【问题标题】:Decode the object array解码对象数组
【发布时间】:2013-06-19 21:30:16
【问题描述】:

(对不起我的英语不好,希望你能理解) 有没有办法解码这个数组? http://status.mc-host.cz/s8.mc-host.cz:25637-feed

使用 Json_decode 它不起作用:/

我需要以这个内容为例:

If ($data['status'] == "online") {
   //something
} Else {
   //offline
}

感谢帮助

【问题讨论】:

  • 这是 PHP print_r() 的结果。 json_decode 解码 JSON。那不是 JSON。
  • 您是从应用程序中打印此数组,还是尝试从 Web 检索此数组并在您的应用程序中使用它?
  • 解码什么?比链接给你一个数组。您只需要使用 for 或 foreach 遍历数组。

标签: php arrays decode


【解决方案1】:

这是 print_r 语句的输出。检查您的情况是否会是这样的最简单方法。

    $online = false;
    $data = file("http://status.mc-host.cz/s8.mc-host.cz:25637-feed", FILE_IGNORE_NEW_LINES);
    foreach($data as $line){
         if (strpos($line, "[Status] => online") !== false){
             $online = true;
         }
    }
    if($online){
       // do something
    } else {
       // offline
    }

值得注意的是,此解决方案不会解码数组,它只是检查您的条件是否满足。

【讨论】:

    【解决方案2】:

    我不确定,但这可能有效:

    function print_r_reverse($in) { 
        $lines = explode("\n", trim($in)); 
        if (trim($lines[0]) != 'Array') { 
            // bottomed out to something that isn't an array 
            return $in; 
        } else { 
            // this is an array, lets parse it 
            if (preg_match("/(\s{5,})\(/", $lines[1], $match)) { 
                // this is a tested array/recursive call to this function 
                // take a set of spaces off the beginning 
                $spaces = $match[1]; 
                $spaces_length = strlen($spaces); 
                $lines_total = count($lines); 
                for ($i = 0; $i < $lines_total; $i++) { 
                    if (substr($lines[$i], 0, $spaces_length) == $spaces) { 
                        $lines[$i] = substr($lines[$i], $spaces_length); 
                    } 
                } 
            } 
            array_shift($lines); // Array 
            array_shift($lines); // ( 
            array_pop($lines); // ) 
            $in = implode("\n", $lines); 
            // make sure we only match stuff with 4 preceding spaces (stuff for this array and not a nested one) 
            preg_match_all("/^\s{4}\[(.+?)\] \=\> /m", $in, $matches, PREG_OFFSET_CAPTURE | PREG_SET_ORDER); 
            $pos = array(); 
            $previous_key = ''; 
            $in_length = strlen($in); 
            // store the following in $pos: 
            // array with key = key of the parsed array's item 
            // value = array(start position in $in, $end position in $in) 
            foreach ($matches as $match) { 
                $key = $match[1][0]; 
                $start = $match[0][1] + strlen($match[0][0]); 
                $pos[$key] = array($start, $in_length); 
                if ($previous_key != '') $pos[$previous_key][1] = $match[0][1] - 1; 
                $previous_key = $key; 
            } 
            $ret = array(); 
            foreach ($pos as $key => $where) { 
                // recursively see if the parsed out value is an array too 
                $ret[$key] = print_r_reverse(substr($in, $where[0], $where[1] - $where[0])); 
            } 
            return $ret; 
        } 
    } 
    

    来自 PHP 手册:http://www.php.net/manual/en/function.print-r.php#93529

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-02-05
      • 2020-03-20
      • 2020-03-14
      • 1970-01-01
      • 1970-01-01
      • 2020-07-01
      • 2017-01-12
      • 2021-08-25
      相关资源
      最近更新 更多