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.

It is my understanding that using isset to prevent duplicate values from being inserted into an array is the best method with regard to memory consumption, resource usage and ease of code processing. I am currently using the array_count_values, like this:

$XMLproducts = simplexml_load_file("products.xml");
foreach($XMLproducts->product as $Product) {
if (condition exists) {
$storeArray[] = (string)$Product->store; //potentially several more arrays will have values stored in them
}}

$storeUniques = array_count_values($storeArray)
foreach ($storeUniques as $stores => $amts) {
?>
<a href="webpage.php?Keyword=<?php echo $keyword; ?>&features=<?php echo $Features; ?>&store=<?php echo $stores; ?>"> <?php echo $stores; ?> </a> <?php echo "(" . ($amts) . ")" . "<br>";
}

How would prevent duplicate values from being inserted into an array (similar to the above) using ISSET? And is there a big performance difference between the 2 if the XML file being parsed is very large (5-6MB)?

share|improve this question
3  
Why not just use array_unique? –  Anthony Apr 11 at 0:55
    
I hear array_unique and array_count_values are quite 'unfriendly' with regard to memory consumption, resource usage and ease of code processing. If there is not a significant performance gap, I will certainly use array_unique or array_count_values –  Dean Olsen Apr 11 at 1:04

3 Answers 3

up vote 0 down vote accepted

As you are using the count in your output, you cannot use array_unique() because you would loose that information.

What you could do, is build the array you need in your loop, using the string as your key and counting the values as you go:

$storeArray = array();
foreach($XMLproducts->product as $Product) {
  if (condition exists) {
    $store = (string)$Product->store;
    if (array_key_exists($store, $storeArray))
    {
       $storeArray[$store]++;
    }
    else
    {
       $storeArray[$store] = 1;
    }
  }
}

Note that this is just to illustrate, you can probably wrap it up in one line.

This way you will not have multiple duplicate strings in your array (assuming that that is your concern) and you don't increase your memory consumption by generating a second (potentially big...) array.

share|improve this answer
    
By the way, I used array_key_exists() but you could easily use isset() instead. –  jeroen Apr 11 at 1:34
    
Generally speaking, would using the array_count_values method negatively impact performance in a significant manner from an end users perspective. We are talking inserting values into an array from parsing a large XML file (large being 5+ MB)! –  Dean Olsen Apr 11 at 1:49
    
@DeanOlsen You would have to measure that, but keep in mind that you are generating a completely new array so if the original array is big, the copy will be smaller, but it will take memory and time to process it. By the way, the size of your XML file is irrelevant in that sense, what counts is the size of the resulting array. –  jeroen Apr 11 at 1:52
    
That does make sense! Thanx! –  Dean Olsen Apr 11 at 1:56

I think array_unique and company are considered unfriendly because they check the database each time an entry is made. The code you're trying to write is doing essentially the same thing, so I don't see a problem with using array_unique.

share|improve this answer

Very simple, no checking required:

foreach($XMLproducts->product as $Product)
    $helperArray[$product->store] = "";

associative arrays have unique keys by definition. if a key already exists, it is simply overwritten.
Now swap key and value:

$storeArray = array_keys($helperArray);

EDIT: to also count the number of occurences of each <store>, I suggest:

foreach($XMLproducts->product as $Product)
    $helperArray[] = (string)$product->store;

And then:

$storeArray = array_count_values($helperArray);

Result: key = unique store, value = count.

share|improve this answer

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.