我不是PHP Guru,我通常使用C#
但我要编辑一些旧的PHP文件。但它大惊小怪。 我的2个数组$ line1& $ line2不断充满相同的数据。 随意查看XML到LINK
我的目标是做到这一点:
第1行; Ping有testid =“7”
第1行; http有testid =“9”
第2行; Ping有testid =“8”
第2行; http有testid =“51”
define("TESTLINE_1", "TDC");
define("TESTLINE_2", "GC");
$line1 = Array();
$line2 = Array();
$xml = WP_CONTENT_DIR.'http://freenats.multimed.dk/api.php?mode=xml&apikey=uig534&query%5b0%5d=test¶m%5b0%5d=7&query%5b1%5d=test¶m%5b1%5d=8&query%5b2%5d=test¶m%5b2%5d=9&query%5b3%5d=test¶m%5b3%5d=51&query%5b4%5d=test¶m%5b4%5d=38&query%5b5%5d=test¶m%5b5%5d=43&query%5b6%5d=test¶m%5b6%5d=41&query%5b7%5d=test¶m%5b7%5d=55';
$feed = simplexml_load_file($xml);
foreach ($feed->test as $test){
if(strpos($test->testname, TESTLINE_1)){
$line1 = AddTestinfoToArray($line1, $test);
}
else if(strpos($test->testname, TESTLINE_2)){
$line2 = AddTestinfoToArray($line2, $test);
}
}
答案 0 :(得分:1)
试试这个希望这会有所帮助。您遗失了[]
,因为没有testname
包含Arrownet
,因此$line2
将为空数组。
<?php
ini_set("display_errors", 1);
define("TESTLINE_1", "TDC");
define("TESTLINE_2", "GC");
$line1 = Array();
$line2 = Array();
$xml = 'http://freenats.multimed.dk/api.php?mode=xml&apikey=uig534&query%5b0%5d=test¶m%5b0%5d=7&query%5b1%5d=test¶m%5b1%5d=8&query%5b2%5d=test¶m%5b2%5d=9&query%5b3%5d=test¶m%5b3%5d=51&query%5b4%5d=test¶m%5b4%5d=38&query%5b5%5d=test¶m%5b5%5d=43&query%5b6%5d=test¶m%5b6%5d=41&query%5b7%5d=test¶m%5b7%5d=55';
$feed = simplexml_load_file($xml);
foreach ($feed->test as $test)
{
if (strpos($test->testname, TESTLINE_1))
{
$line1[] = (string)$test->testname;//added this line
} else if (strpos($test->testname, TESTLINE_2))
{
$line2[] = (string)$test->testname;//added this line
}
}
print_r($line1);
print_r($line2);
答案 1 :(得分:1)
试试这个我希望它能满足您的需求
ini_set("display_errors", 1);
$line1 = Array();
$line2 = Array();
$xml = 'http://freenats.multimed.dk/api.php?mode=xml&apikey=uig534&query%5b0%5d=test¶m%5b0%5d=7&query%5b1%5d=test¶m%5b1%5d=8&query%5b2%5d=test¶m%5b2%5d=9&query%5b3%5d=test¶m%5b3%5d=51&query%5b4%5d=test¶m%5b4%5d=38&query%5b5%5d=test¶m%5b5%5d=43&query%5b6%5d=test¶m%5b6%5d=41&query%5b7%5d=test¶m%5b7%5d=55';
$feed = simplexml_load_file($xml);
foreach ($feed->test as $test)
{
if (strpos($test->testname, "TDC"))
{
if(strpos($test->testname, 'pingtest')){
$line1[] = 'Ping has testid='.(string)$test->testid;//added this line
}else{
$line1[] = 'Http has testid='.(string)$test->testid;//added this line
}
} else if (strpos($test->testname, "GC"))
{
if(strpos($test->testname, 'pingtest')){
$line2[] = 'Ping has testid='.(string)$test->testid;//added this line
}else{
$line2[] = 'Http has testid='.(string)$test->testid;//added this line
}
}
}
print_r($line1);
print_r($line2);