移动到PHP5.4后,阵列不工作
编辑:我在这篇文章的底部添加了我的全部代码,下面的代码在PHP5.3中运行良好,但在PHP5.4中返回空结果是什么原因 这是我正在使用的代码:移动到PHP5.4后,阵列不工作,php,arrays,Php,Arrays,编辑:我在这篇文章的底部添加了我的全部代码,下面的代码在PHP5.3中运行良好,但在PHP5.4中返回空结果是什么原因 这是我正在使用的代码: $xaxis=array(); for ($i = 1; $i <= 3; $i++) { $chachg_time = $xml->xpath("//ns:BulkData[ns:Name='ChannelChangeHistory.{$i}.ChannelChangeTime']/ns:Value");
$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];
}
在PHP5.4中,我得到以下信息:
Array
(
[0] => SimpleXMLElement Object
(
)
)
Array
(
[0] => SimpleXMLElement Object
(
)
[1] => SimpleXMLElement Object
(
)
)
Array
(
[0] => SimpleXMLElement Object
(
)
[1] => SimpleXMLElement Object
(
)
[2] => SimpleXMLElement Object
(
)
)
以下是PHP5.3版本的原始工作代码
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();
?>
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)$/';/只检查带有这些ext的文件。
$newstamp=0;
$newname=“”;
如果($handle=opendir($dir)){
而(false!==($fname=readdir($handle)){
//消除当前目录、父目录
如果(preg_match('/^\.{1,2}$/',$fname))继续;
//消除不在模式中的其他页面
如果(!preg_match($pattern,$fname))继续;
$timedat=filemtime($dir/$fname”);
如果($timedat>$newstamp){
$newst坦p=$timedat;
$newname=$fname;
}
}
}
closedir($handle);
$feed=file\u get\u contents($newname);
$xml=新的simplexmlement($feed);
$xml->registerXPathNamespace(“ns”,“urn:宽带论坛组织:ipdr:tr-232-1-0”);
//运行频道更改历史记录脚本//
$yaxis=array();
$xaxis=array();
对于($i=1;$i xpath(//ns:BulkData[ns:Name='InternetGatewayDevice.LANDevice.1.WLANConfiguration.1.X_ATT_ChannelChangeHistory.{$i}.ChannelChangeTime']/ns:Value”);
$chachg_time=$chachg_time[0];
如果($chachg_time==“”)
{
打破
}
否则{
$xaxis[]=日期(“M-d h:i:s A”,标准时间($chachg_时间[0]);
$ch=$xml->xpath(//ns:BulkData[ns:Name='InternetGatewayDevice.landDevice.1.WLANConfiguration.1.X_ATT_ChannelChangeHistory.{$i}.Channel']/ns:Value”);
$ch=$ch[0];
$yaxis[]=$ch[0];
}
}
foreach($xaxis为$k=>&$v){
$a=(数组)$v;
$v=$a[0];
}
foreach($yaxis作为$s=>&$t){
$b=(数组)$t;
$t=$b[0];
}
//图形的宽度和高度
$width=1000;$height=800;
//创建一个图形实例
$graph=新图形($width,$height);
$graph->setMargin(50,50,40200);
//指定要使用的比例,
//text=X轴的txt比例
//int=Y轴的整数比例
$graph->SetScale(“textint”,0,50);
//设置图表的标题
$graph->title->Set(“”);
//设置标题和X轴标签
$graph->xaxis->title->Set(“”);
$graph->xaxis->SetTickLabels($xaxis);
//$graph->xaxis->scale->SetDateFormat('H:i');
$graph->xaxis->SetLabelAngle(90);
$graph->xaxis->SetFont(FF_FONT2);
//设置Y轴标题
$graph->yaxis->title->Set('Channel');
$graph->yaxis->scale->SetAutoMax(11);
$graph->yaxis->title->SetFont(FF_FONT2);
$graph->yaxis->SetTitlemargin(25);
//创建条形图
$sp1=新散点图($yaxis);
$sp1->mark->SetType(标记填充圆);
$sp1->mark->SetFillColor(“61a9f3”);
$sp1->标记->设置宽度(8);
//将绘图添加到图形中
$graph->Add($sp1);
//显示图形
$graph->Stroke();
?>
您的结果不是空的,只有转储的SimpleXMLElement
不会显示任何有用的数据
从这些对象访问和转储数据,您将看到
有关更多详细信息,请参见中的注释。在我看来还行(因为您得到的是
simplexmlement
对象)。在SimpleXMLElement
上使用var\u dump
或print\u r
实际上是没有用的,所以不要依赖它来进行实际的数据/调试。我怀疑这是您的问题,可能是代码的另一部分导致了问题。@mamdouh alramadan,我包括了完整的代码。@Phil还有什么其他方法可以调试它?@user2755910为什么需要调试?只需查看XML并使用简单的XML库即可
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();
?>