Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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!

share|improve this question
    
Leading/trailing whitespace? –  knittl Mar 18 at 12:26
1  
Cast $Product->sku as a string before doing the comparison. –  Amal Murali Mar 18 at 12:29
    
can you check the value of $Product->sku because you might be pointing to non-existing keys in multipleSku array –  krishna Mar 18 at 12:39
    
Thanks! It now works! –  Dean Olsen Mar 18 at 23:07

1 Answer 1

up vote 0 down vote accepted

try this

foreach($XMLproducts->product as $Product) { 
  $sku = (string) $Product->sku;
  if ($multipleSku[$sku] > 1) {
    echo $Product->sku;
  }
}
share|improve this answer
    
Thanks! It worked! –  Dean Olsen Mar 18 at 23:06

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.