Which of these two methods (array_count_values or array_key_exists) is more 'friendly' with regard to memory consumption, resource usage and ease of code processing. And is the difference big enough to where a significant performance bump or loss could take place?
If more details on how these methods may be used are needed, I will be parsing a very large XML file and using simpleXML to retrieve certain XML values into an array (to store URL's), 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
}}
No duplicate values will be inserted into the array, and I am debating on either using the array_key_exists or array_count_values method to prevent duplicate values. I understand array_key_exists is more , but my question is how much more resource friendly is it? Enough to slow down performance significantly?
I would much rather use the array_count_values method primarily because along with preventing duplicate value in the array, you can also easily display how many duplicate values are in each value. IE if in the storeArray "Ace Hardware" had 3 instances, you could easily display "3" next to the URL, like this:
// The foreach loop builds the arrays with values from the XML file, like this:
$storeArray2[] = (string)$Product->store;
// Then we later display the filter links like this:
$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>";
}
If there is a big performance gap between the 2 methods, I will go with the array_key_exists method. But the code below only prevents duplicate values from being displayed. Is there a way to also display the number of duplicate values (for each value) that would have occurred?
// The foreach loop builds the arrays with values from the XML file, like this:
$Store = (string)$Product->store;
if (!array_key_exists($Store, $storeArray)) {
$storeArray[$Store] = "<a href='webpage.php?Keyword=".$keyword."&Features=".$features."&store=".$Product->store"'>" . $Store . "</a>";
}
// Then we later display the filter links like this:
foreach ($storeArray as $storeLinks) {
echo $storeLinks . "<br>";
}