The skuArray
is being populated with values from an XML file, using the simplexml_load_file
method and the first foreach
loop below:
$XMLproducts = simplexml_load_file("products.xml");
$skuArray = array();
foreach($XMLproducts->product as $Product) {
$skuArray[] = (string)$Product->sku; // Product->sku contains the sku value obtained from the XML file
}
In this case, the values inserted into the skuArray
are (5, 7, 3, 7, 1, 5, 7)
.
After the array is populated, we then check to see if any duplicate values exist in the skuArray
using array_count_values
method. If so, an if
statement executes some code.
$multipleSku = array_count_values($skuArray);
The if
statement in this foreach
loop is NOT executing the code, even when multiple values exist in the array (5 and 7 are in this array multiple times).
foreach($XMLproducts->product as $Product) {
if ($multipleSku[$Product->sku] > 1) {
echo $Product->sku;
}
}
The code looks to be written correctly! Any advice? Thanks!
$Product->sku
as a string before doing the comparison. – Amal Murali Mar 18 at 12:29$Product->sku
because you might be pointing to non-existing keys inmultipleSku
array – krishna Mar 18 at 12:39