PHP-数组\计数\值或数组\键\存在

PHP-数组\计数\值或数组\键\存在,php,xml,array-key-exists,Php,Xml,Array Key Exists,这两种方法(array\u count\u value或array\u key\u exists)中哪一种在内存消耗、资源使用和代码处理的易用性方面更“友好”。这种差异是否足够大,以至于可能发生重大性能提升或损失 如果需要更多关于如何使用这些方法的详细信息,我将解析一个非常大的XML文件,并使用simpleXML将某些XML值检索到数组中(以存储URL),如下所示: $XMLproducts = simplexml_load_file("products.xml"); foreach($XMLp

这两种方法(array\u count\u value或array\u key\u exists)中哪一种在内存消耗、资源使用和代码处理的易用性方面更“友好”。这种差异是否足够大,以至于可能发生重大性能提升或损失

如果需要更多关于如何使用这些方法的详细信息,我将解析一个非常大的XML文件,并使用simpleXML将某些XML值检索到数组中(以存储URL),如下所示:

$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
}}
// 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>";
}
不会在数组中插入重复的值,我正在讨论使用array\u key\u exists或array\u count\u values方法来防止重复值。我知道array\u key\u的存在更多,但我的问题是它对资源的友好程度有多高?是否足以显著降低性能

我更愿意使用array\u count\u values方法,主要是因为除了防止数组中的重复值外,还可以轻松显示每个值中有多少重复值。IE如果在storeArray中“Ace硬件”有3个实例,您可以很容易地在URL旁边显示“3”,如下所示:

$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
}}
// 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>";
}
//foreach循环使用XML文件中的值构建数组,如下所示:
$storeArray2[]=(字符串)$Product->store;
//然后,我们将在稍后显示过滤器链接,如下所示:
$storeUniques=数组\计数\值($storeArray)
foreach($storeUniques作为$stores=>$amts){
?>
储存;
如果(!array_key_存在($Store,$storeArray)){
$storeArray[$Store]=“”;
}

//然后我们将在后面显示如下筛选器链接:
foreach($storeArray作为$storeLinks){
echo$storeLinks.“
”; }
使用isset()! 它比存在的数组键快2.5倍

资料来源: