0
아래 코드는 아래 XML 파일에서 "store"요소의 값을 검색하고 배열 (storeArray)에 값을 삽입하는 데 사용됩니다. 중복 값을 배열에 넣고 싶지 않습니다. IE는 Best Buy가 두 번 삽입되는 것을 원하지 않습니다. 중복을 막기 위해 in_array 메서드 을 사용하고 있습니다.PHP의 in_array 함수가 XML과 올바르게 작동하지 않습니다.
중복을 방지하지 않습니다이 코드 (IE 베스트 바이 (Best Buy)가 두 번 표시됩니다) :
$xmlDoc = simplexml_load_file("products.xml");
$storeArray = array();
foreach($xmlDoc->product as $Product) {
echo "Name: " . $Product->name . ", ";
echo "Price: " . $Product->price . ", ";
if(!in_array($Product->store, $storeArray)) {
$storeArray[] = $Product->store;
}
}
foreach ($storeArray as $store) {
echo $store . "<br>";
}
그러나이 코드는 작업을 수행합니다
$practiceArray = array('Product1', 'Product2', 'Product3');
if (in_array("Product1", $practiceArray)) {
echo "Is in array";}
else {
echo "NOT in array";
}
어떤 조언을?
<products>
<product type="Electronics">
<name> Desktop</name>
<price>499.99</price>
<store>Best Buy</store>
</product>
<product type="Electronics">
<name>Lap top</name>
<price>599.99</price>
<store>Best Buy</store>
</product>
<product type="Hardware">
<name>Hand Saw</name>
<price>99.99</price>
<store>Lowes</store>
</product>
</products>
문자열 외에 초기에 변환 할 수있는 변수 유형은 무엇입니까? –
@SetSailMedia 어떤 유형 (배열) (bool) (문자열)도 캐스트 할 수 있습니다 ... 결과는 예상 한 결과입니다. :) –
고마워요! 그것은 효과가있다! –