移动到PHP5.4后,数组不起作用

时间:2013-11-22 06:33:20

标签: php arrays

编辑:我在这篇文章的底部添加了我的整个代码我的代码在PHP 5.3中运行良好,但它在PHP 5.4中返回空结果是什么导致它?

这是我正在使用的代码:

$xaxis=array();

    for ($i = 1; $i <= 3; $i++) {
        $chachg_time = $xml->xpath("//ns:BulkData[ns:Name='ChannelChangeHistory.{$i}.ChannelChangeTime']/ns:Value");
        $chachg_time = $chachg_time[0]; 
        $xaxis[] = $chachg_time[0];
     }      

在PHP 5.3中,我得到以下结果(这是我所期待的):

Array
(
    [0] => SimpleXMLElement Object
        (
            [0] => 6
        )

)
Array
(
    [0] => SimpleXMLElement Object
        (
            [0] => 6
        )

    [1] => SimpleXMLElement Object
        (
            [0] => 5
        )

)
Array
(
    [0] => SimpleXMLElement Object
        (
            [0] => 6
        )

    [1] => SimpleXMLElement Object
        (
            [0] => 5
        )

    [2] => SimpleXMLElement Object
        (
            [0] => 3
        )

)

在PHP 5.4中,我得到以下内容:

Array
(
    [0] => SimpleXMLElement Object
        (
        )

)
Array
(
    [0] => SimpleXMLElement Object
        (
        )

    [1] => SimpleXMLElement Object
        (
        )

)
Array
(
    [0] => SimpleXMLElement Object
        (
        )

    [1] => SimpleXMLElement Object
        (
        )

    [2] => SimpleXMLElement Object
        (
        )

)

以下是php 5.3 Version

的原始workig代码
require_once ('jpgraph/src/jpgraph.php');
require_once ('jpgraph/src/jpgraph_line.php');
require_once ('jpgraph/src/jpgraph_scatter.php');
require_once ('jpgraph/src/jpgraph_date.php');


$dir = realpath(getcwd());
$pattern = '/\.(xml)$/'; // check only file with these ext.          
$newstamp = 0;            
$newname = "";

if ($handle = opendir($dir)) {               
       while (false !== ($fname = readdir($handle)))  {            
         // Eliminate current directory, parent directory
         if (preg_match('/^\.{1,2}$/',$fname)) continue;            
         // Eliminate other pages not in pattern            
         if (! preg_match($pattern,$fname)) continue;            
         $timedat = filemtime("$dir/$fname");            
         if ($timedat > $newstamp) {
            $newstamp = $timedat;
            $newname = $fname;
          }
         }
        }
closedir ($handle);

$feed = file_get_contents($newname);
$xml = new SimpleXmlElement($feed);
$xml->registerXPathNamespace("ns", "urn:broadband-forum-org:ipdr:tr-232-1-0");

// RUN CHANNEL CHANGE HISTORY SCRIPT // 

        $yaxis=array();
        $xaxis=array();

        for ($i = 1; $i <= 32; $i++) {
                $chachg_time = $xml->xpath("//ns:BulkData[ns:Name='InternetGatewayDevice.LANDevice.1.WLANConfiguration.1.X_ATT_ChannelChangeHistory.{$i}.ChannelChangeTime']/ns:Value");
                $chachg_time = $chachg_time[0]; 
                if ($chachg_time == "") 
                    {
                    break;
                    }       
                else {

                    $xaxis[] = date("M-d h:i:s A", strtotime($chachg_time[0]));
                    $ch = $xml->xpath("//ns:BulkData[ns:Name='InternetGatewayDevice.LANDevice.1.WLANConfiguration.1.X_ATT_ChannelChangeHistory.{$i}.Channel']/ns:Value");
                    $ch = $ch[0];
                    $yaxis[]=$ch[0];
                    }
                }               
                foreach ($xaxis as $k => &$v) {
                    $a = (array)$v;
                    $v = $a[0];
                }
                foreach ($yaxis as $s => &$t) {
                    $b = (array)$t;
                    $t = $b[0];
                }



// Width and height of the graph
$width = 1000; $height = 800;

// Create a graph instance
$graph = new Graph($width,$height);

$graph->setMargin(50,50,40,200);
// Specify what scale we want to use,
// text = txt scale for the X-axis
// int = integer scale for the Y-axis
$graph->SetScale("textint",0,50);

// Setup a title for the graph
$graph->title->Set('');

// Setup titles and X-axis labels
$graph->xaxis->title->Set('');
$graph->xaxis->SetTickLabels($xaxis);
//$graph->xaxis->scale->SetDateFormat('H:i');
$graph->xaxis->SetLabelAngle(90);
$graph->xaxis->SetFont(FF_FONT2);

// Setup Y-axis title
$graph->yaxis->title->Set('Channel');
$graph->yaxis->scale->SetAutoMax(11); 
$graph->yaxis->title->SetFont(FF_FONT2);
$graph->yaxis->SetTitlemargin(25);
 // Create the bar plot

$sp1 = new ScatterPlot($yaxis);
$sp1->mark->SetType(MARK_FILLEDCIRCLE);
$sp1->mark->SetFillColor("#61a9f3");
$sp1->mark->SetWidth(8);


// Add the plot to the graph
$graph->Add($sp1);

// Display the graph
$graph->Stroke();

?>

1 个答案:

答案 0 :(得分:1)

您的结果不为空,只有转储SimpleXMLElement不会显示任何有用的数据。

从这些对象访问和转储数据,你会看到。

有关详情,请参阅SimpleXMLElement documentation中的评论。