删除数组中与相同字母(ONLY)值匹配的元素的第一个实例之后的所有元素


Remove all elements in an array after the first instance of an element that matches same alphabetic (ONLY) value

我需要删除数组中位于点.之前匹配相同字符串值的元素的第一个实例之后的所有元素,即不考虑. 之后的任何值

来自

$array = ("ItemNew1.1", "Item2.0", "Item3Test.0", "Item2.2", "Item4.4", "Item2.5")

$array = ("ItemNew1.1", "Item2.0", "Item3Test.0", "Item4.4")

下面的代码创建一个临时数组来保存数组中已经存在的值,它在原始数组上运行foreach,如果值不在临时数组中,它会将其插入到新的数组中

$tempArray = array();
$newArray = array();
foreach($array as $value) {
    list($item, ) = explode(".", $value);
    $int = filter_var($item, FILTER_SANITIZE_NUMBER_INT);
    if(!in_array($int, $tempArray)) {
        $newArray[] = $value;
        $tempArray[] = $int;
    }
}

现在,$newArray就是您想要的数组。

DEMO