php根据多维数组的第一个值删除重复项

标签 php multidimensional-array duplicate-removal

给定

[0] => Array
    (
        [0] => ask.com
        [1] => 2320476
    )

[1] => Array
    (
        [0] => amazon.com
        [1] => 1834593
    )

[2] => Array
    (
        [0] => ask.com
        [1] => 1127456
    )

我需要仅根据第一个值删除重复值,而不管任何其他后续值是什么。请注意 [0][1] 与 [2][1] 不同,但我认为这是重复的,因为有两个匹配的第一个值。其他数据无关紧要,不应进行比较。

最佳答案

试试这个,假设 $mainArray 是您拥有的数组。

$outputArray = array(); // The results will be loaded into this array.
$keysArray = array(); // The list of keys will be added here.
foreach ($mainArray as $innerArray) { // Iterate through your array.
    if (!in_array($innerArray[0], $keysArray)) { // Check to see if this is a key that's already been used before.
        $keysArray[] = $innerArray[0]; // If the key hasn't been used before, add it into the list of keys.
        $outputArray[] = $innerArray; // Add the inner array into the output.
    }
}
print_r($outputArray);

关于php根据多维数组的第一个值删除重复项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13922633/

相关文章:

C++ : 2-D Pointer Array Sorting: Selection Sort doesn't work for certain instance

php - 似乎无法让 $_GET ['id' ] 工作

javascript - jQuery AJAX - 当加载方法失败时重定向到 404 页面

python - 2D NumPy 数组的蛇形遍历

javascript - 使用lodash javascript全连接二维数组

php - MySQL:在不破坏外键约束的情况下消除重复行

solr - 搜索 Solr 索引时避免重复

mysql - 如何从 mysql 数据库中删除重复的条目?

php - AH01276 : Cannot serve directory/var/www/html/: No matching DirectoryIndex (index. php,index.html) 找到

php - WordPress 自定义查询不返回结果