解析XML文件,跳过一个标记


Parsing XML file, one tag is skipped

我正在尝试加载xml文件并解析它。我有一个配置文件,在那里我定义了我感兴趣的名称数组,所以加载文件后,我比较标签与我预定义的数组,如果值匹配,然后使用标签(名称)作为索引到数组和保存其值。问题是,它总是跳过在配置数组中第一个名称的标记。此外,LoadCsvReport应该工作,除了少数错误检查是好的代码吗?我正在学习面向对象编程,但我认为我没有使用它。

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",
);

问题就在这里:

elseif (array_search($key, $bing))

函数arrach_search返回找到传递值的数组索引。对于数组中的第一项,即0。但是if(0)等于if(false)

你需要区分返回值0对于"I 've found the item in the array in the第0个位置"和返回值false对于"I haven't found the item in the array in the all":

正确的:

elseif  (array_search($key, $bing) !== FALSE)