更高效的重复数据阵列过滤

时间:2018-05-09 15:11:18

标签: php arrays

我正在学习编写PHP代码并给自己一个挑战。通过匹配重复的特定元素值来过滤数组数据。我在纸上逻辑地阅读然后编写代码并且它首先尝试:)但我敢打赌它是最低效的方式所以学习我想将它与其他人做的比较。这是我的代码。该数组只是一些元素,如name和id。我的结果是一个列表,其中包含具有许多重复ID但名称不同的数组中具有唯一ID的项目。哦,挑战的一部分是测试XML,JSON和Arrays,所以我制作了原始数据XML并使用了SimpleXML,但是将这些数据复制为PHP数组并使用它来进行过滤(接下来是XPath挑战)。

感谢您查看以及我可以学习的任何建议的改进。

//$items is a SimpleXML array of items with name and id elements with many duplicate ids but different names.

//first iteration of id collection
$ids = array();
$countedIds = array();
//convert simplexml array $items to normal array (copy)
$itemsArray = json_decode(json_encode($items), TRUE);           
foreach ($itemsArray as $idItem) 
{
    //collection all ids
    $ids[] = $idItem['id'];     
}
//count number of each id and store in separate array
$countedIds = array_count_values($ids);
#print_r($countedIds);

//start cleaning out duplicates
foreach($itemsArray as $key => &$item)
{
    //clear any whitespace (thanks internets)
    #$thisid = trim($item->id); //if using simplexml
    $thisid = trim($item['id']);
    //check if this id is duplicated (multiple copies) in array
    if($countedIds[$thisid] >1)
    {
        echo $thisid . "item has duplicates (" . $countedIds[$thisid] . ")<br />";
        //add found dupes to separate array
        $dupes[] = $thisid;
        //remove this item from $itemsArray and original $items simplexml array
        unset($itemsArray[$key]);
        unset($items[$key]);
        echo "removed element index " . $key . $item . "<br /><hr>";                    
        //remove this id from $ids array
        unset($ids[$key]);
        //clear out the counted ids
        unset($countedIds);
        //make a fresh array for counting ids
        $countedIds = array();
        //get updated id count
        $countedIds = array_count_values($ids);

        //test simplexml unset?
        #unset($sxe->$this->channel->item);
    }
}       
//review results
print_r($countedIds);
print_r($ids);
print_r($dupes);
print_r($itemsArray);
print_r($items);

0 个答案:

没有答案