我正在尝试加载xml文件并解析它。我有一个配置文件,我在其中定义了我感兴趣的名称数组,所以在加载文件后我将标签与我预定义的数组进行比较,如果值匹配,则使用标签(名称)作为数组的索引并保存其值。问题是它总是在名称中跳过标记,这是配置数组中的第一个。此外,LoadCsvReport
应该可以工作,除了很少的错误检查是好的代码吗?我正在学习OOP,但我想我不会使用任何一个。
class LoadAdReport extends CI_Controller
{
/*
* @return array[][]
* */
public function LoadCsvReport()
{
require "config.php";
$key = array();
$values = array();
$flag = false;
$csvfile = fopen(dirname(__FILE__) . "/result.csv", "r");
while ($file = fgetcsv($csvfile)) {
if (!$flag) {
/*Loop through config array which contains names of columns of our interest
If row from file contains name from config, then that name will have assigned
index of that column
*/
foreach ($bing as $name)
if ($value = array_search($name, $file)) {
$key[$name] = $value;
$flag = true;
}
//After column indexes are assigned to names skip current row
if ($flag)
continue;
}
if ($flag) {
foreach ($key as $columnName => $index) {
/*Get columnname and index, items from $file[$index] are assign to array
corresponding array with columnName as index
*/
if ($file[$index] === "-")
break;
$values[$columnName][] = $file[$index];
//$key[$index] = array($item => $file[$item]);
}
}
}
foreach ($values as $key => $val)
foreach ($val as $lol => $item)
echo $key . "=>" . $item . "<br/>";
fclose($csvfile);
}
public function LoadXmlReport()
{
require "config.php";
$items = array();
$xmlfile = simplexml_load_file(dirname(__FILE__) . "/xmlfile.xml");
foreach ($xmlfile as $key => $value)
foreach ($value as $key => $item) {
if ($item == "-") {
break;
} elseif (array_search($key, $bing))
$items[$key][] = $item;
}
foreach ($items as $key => $val)
foreach ($val as $lol => $item)
echo $key . "=>" . $item . "<br/>";
}
}
这是我的XML文件
<root>
<row>
<Status>Enabled</Status>
<Keyword>Toaletna voda</Keyword>
<Campaign>Lešenari</Campaign>
<Adgroup>Lešenaris</Adgroup>
<BidStrategyType>InheritFromParent</BidStrategyType>
<Bid>0.05</Bid>
<Matchtype>Broad</Matchtype>
<Clicks>0</Clicks>
<Impr.>0</Impr.>
<Conv.>0</Conv.>
</row>
<row>
<Status>Enabled</Status>
<Keyword>lyžička</Keyword>
<Campaign>Lešenari</Campaign>
<Adgroup>Lešenaris</Adgroup>
<BidStrategyType>InheritFromParent</BidStrategyType>
<Bid>0.05</Bid>
<Matchtype>Broad</Matchtype>
<Clicks>0</Clicks>
<Impr.>0</Impr.>
<Conv.>0</Conv.>
</row>
<row>
<Status>Search total</Status>
<Keyword>-</Keyword>
<Campaign>-</Campaign>
<Adgroup>-</Adgroup>
<BidStrategyType>-</BidStrategyType>
<Bid>-</Bid>
<Matchtype>-</Matchtype>
<Clicks>0</Clicks>
<Impr.>0</Impr.>
<Conv.>0</Conv.>
</row>
<row>
<Status>Content total</Status>
<Keyword>-</Keyword>
<Campaign>-</Campaign>
<Adgroup>-</Adgroup>
<BidStrategyType>-</BidStrategyType>
<Bid>-</Bid>
<Matchtype>-</Matchtype>
<Clicks>0</Clicks>
<Impr.>0</Impr.>
<Conv.>0</Conv.>
</row>
<row>
<Status>Deleted items total</Status>
<Keyword>-</Keyword>
<Campaign>-</Campaign>
<Adgroup>-</Adgroup>
<BidStrategyType>-</BidStrategyType>
<Bid>-</Bid>
<Matchtype>-</Matchtype>
<Clicks>0</Clicks>
<Impr.>0</Impr.>
<Conv.>0</Conv.>
</row>
<row>
<Status>Overall total</Status>
<Keyword>-</Keyword>
<Campaign>-</Campaign>
<Adgroup>-</Adgroup>
<BidStrategyType>-</BidStrategyType>
<Bid>-</Bid>
<Matchtype>-</Matchtype>
<Clicks>0</Clicks>
<Impr.>0</Impr.>
<Conv.>0</Conv.>
</row>
</root>
这是我的配置文件
$bing = array(
"Adgroup",
"Campaign",
"Keyword",
"Clicks",
"Impr.",
"Conv.",
"Bid",
);
答案 0 :(得分:1)
问题在于:
elseif (array_search($key, $bing))
函数arrach_search
返回找到传递值的数组索引。对于数组中的第一项,即0
。但if(0)
等于if(false)
。
您需要区分“我在数组中第0个位置找到项目”的返回值0
和“我还没找到项目”的返回值false
在数组中所有“:
正确:
elseif (array_search($key, $bing) !== FALSE)