0

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!

3
  • Leading/trailing whitespace? Commented Mar 18, 2014 at 12:26
  • 1
    Cast $Product->sku as a string before doing the comparison. Commented Mar 18, 2014 at 12:29
  • can you check the value of $Product->sku because you might be pointing to non-existing keys in multipleSku array Commented Mar 18, 2014 at 12:39

1 Answer 1

0

try this

foreach($XMLproducts->product as $Product) { 
  $sku = (string) $Product->sku;
  if ($multipleSku[$sku] > 1) {
    echo $Product->sku;
  }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.