rtom rtom - 26 days ago 5
PHP Question

Parsing XML file, one tag is skipped

I am trying to load an xml file and parse it. I have an config file where I defined array with names that I am interested in, so after loading the file I compare the tags with my predefined array and if the value matches then use the tag(name) as index into array and save its value. Problem is that it is always skipping the tag with name that is first in config array. Also,

LoadCsvReport
should work, except few error checking is it good code ? I am learning OOP, but i think i don't use any of it.

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/>";


}
}


This is my XML file

<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>


and this is my config file

$bing = array(
"Adgroup",
"Campaign",
"Keyword",
"Clicks",
"Impr.",
"Conv.",
"Bid",
);

Answer

The problem is here:

elseif (array_search($key, $bing))

The function arrach_search returns the array index where the passed value was found. For the first item in the array, that is 0. But if(0) equals to if(false).

You need to differentiate between the return value 0 for "I 've found the item at the 0th position in the array" and the return value false for "I haven't found the item in the array at all":

Correct:

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