使用array_search PHP将值映射到CSV标头列

时间:2014-01-13 16:35:20

标签: php arrays csv mapping

我正在使用PHP格式化XML文件,该文件通过SOAP请求获取并将其转换为CSV。 基于XML的值我正在创建一个CSV标头,并通过使用array_search比较数组中的值,我将值映射到右列。这适用于前几个,但由于某些行不包含所有列的值,因此事情会被切换。

当没有可用于该列的值时,我需要一种方法将列留空。

$spec = $product->thirdPartyInfo->specifications->specification;
  if (is_array($spec)){
    foreach($spec as $specification){
       $texualDescription = $specification->textualDescription->_;
       $value = $specification->value->_;

       $place = array_search($texualDescription, $header);
       $row[$place] = $value;
    }
}

发生的事情是第一列被填充,最后一个或第四列完全为空,而它们应该有某些行的某些值。

我希望我已经足够清楚地解释了它。

提前致谢。

编辑:

这是原始XML ...

<ans1:specifications>
                    <ans1:specification id="T0000002B3645065">
                       <ans1:textualDescription lang="NL">Productbeschrijving</ans1:textualDescription>
                       <ans1:value lang="NL">Electronic HP Care Pack Next Business Day Proactive Care Service with Comprehensive Defective Material Retention - uitgebreide serviceovereenkomst - 3 jaren - ter plaatse</ans1:value>
                    </ans1:specification>
                    <ans1:specification id="T0000026B0000028">
                       <ans1:textualDescription lang="NL">Type</ans1:textualDescription>
                       <ans1:value lang="NL">Uitgebreide serviceovereenkomst</ans1:value>
                    </ans1:specification>
                    <ans1:specification id="T0000027B0000029">
                       <ans1:textualDescription lang="NL">Onderhoud inbegrepen</ans1:textualDescription>
                       <ans1:value lang="NL">Onderdelen en werkuren</ans1:value>
                    </ans1:specification>
                    <ans1:specification id="T0000113B0000622">
                       <ans1:textualDescription lang="NL">Locatie</ans1:textualDescription>
                       <ans1:value lang="NL">Ter plaatse</ans1:value>
                    </ans1:specification>
                    <ans1:specification id="T0000028B0000030">
                       <ans1:textualDescription lang="NL">Volledige contractperiode</ans1:textualDescription>
                       <ans1:value lang="NL">3 jaren</ans1:value>
                    </ans1:specification>
                    <ans1:specification id="T0000172B0082957">
                       <ans1:textualDescription lang="NL">Responstijd</ans1:textualDescription>
                       <ans1:value lang="NL">Volgende werkdag</ans1:value>
                    </ans1:specification>
                    <ans1:specification id="T0000599B0787166">
                       <ans1:textualDescription lang="NL">Bereikbaarheid van servicedienst</ans1:textualDescription>
                       <ans1:value lang="NL">9 uur per dag / 5 dagen per week</ans1:value>
                    </ans1:specification>
                    <ans1:specification id="T0001742B2995742">
                       <ans1:textualDescription lang="NL">Ontworpen voor</ans1:textualDescription>
                       <ans1:value lang="NL">HP E4210-24 Switch, E4500-24 Switch, E4500-24G Switch, E4500-24G-PoE Switch, E4500-24-PoE Switch, E4500-48 Switch, E4500-48G-PoE Switch, E4500-48-PoE Switch, E4510-48G Switch</ans1:value>
                    </ans1:specification>
                 </ans1:specifications>

0 个答案:

没有答案