基于值(而不是数组键)唯一的多维数组

发布于 2024-11-08 06:37:21 字数 1388 浏览 0 评论 0原文

我有一个多维数组,我需要对其进行唯一性排序,因为我有重复的记录,因此我需要 array_unique 遍历数组并按值删除重复项,例如

Array
(
    [0] => Array
        (
            [id] => 324
            [time_start] => 1301612580
            [level] => 0.002
            [input_level] => 0.002
        )

    [1] => Array
        (
            [id] => 325
            [time_start] => 1301612580
            [level] => 0.002
            [input_level] => 0.002
        )

    [2] => Array
        (
            [id] => 326
            [time_start] => 1301612580
            [level] => 0.002
            [input_level] => 0.002
        )
)

有重复的 time_start,它们都是相同的,还有 levelinput_level,但只有当有匹配的 time_start 时,它们才不会受到影响> 它应该删除它并处理整个数组(该数组比您想象的要大,但我只是发布了该数组的一个小示例)。应该删除重复项并像这样返回:

Array
(
    [0] => Array
        (
            [id] => 324
            [time_start] => 1301612580
            [level] => 0.002
            [input_level] => 0.002
        )
)

我发现不起作用的问题:

I have a multidimensional array which I need to be sorted with uniqueness as I have duplicated records, so I need array_unique to go through the array and remove duplicates by the value, e.g.

Array
(
    [0] => Array
        (
            [id] => 324
            [time_start] => 1301612580
            [level] => 0.002
            [input_level] => 0.002
        )

    [1] => Array
        (
            [id] => 325
            [time_start] => 1301612580
            [level] => 0.002
            [input_level] => 0.002
        )

    [2] => Array
        (
            [id] => 326
            [time_start] => 1301612580
            [level] => 0.002
            [input_level] => 0.002
        )
)

There are duplicated time_start, which they are all the same, also level and input_level but they are not to be affected, only if there are matching time_start it should remove it and process the whole array (the array is bigger than you think, but I just posted a small example of the array). Should remove dupes and return like this:

Array
(
    [0] => Array
        (
            [id] => 324
            [time_start] => 1301612580
            [level] => 0.002
            [input_level] => 0.002
        )
)

Questions I've found that didn't work:

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

歌枕肩 2024-11-15 06:37:22
$input = array( /* your data */ );
$temp  = array();
$keys  = array();

foreach ( $input as $key => $data ) {
    unset($data['id']);
    if ( !in_array($data, $temp) ) {
        $temp[]     = $data;
        $keys[$key] = true;
    }
}

$output = array_intersect_key($input, $keys);

或者

$input = array( /* your data */ );
$temp  = $input;

foreach ( $temp as &$data ) {
    unset($data['id']);
}

$output = array_intersect_key($input, array_unique($temp));
$input = array( /* your data */ );
$temp  = array();
$keys  = array();

foreach ( $input as $key => $data ) {
    unset($data['id']);
    if ( !in_array($data, $temp) ) {
        $temp[]     = $data;
        $keys[$key] = true;
    }
}

$output = array_intersect_key($input, $keys);

or

$input = array( /* your data */ );
$temp  = $input;

foreach ( $temp as &$data ) {
    unset($data['id']);
}

$output = array_intersect_key($input, array_unique($temp));
如歌彻婉言 2024-11-15 06:37:22
$temp = array();
array_filter($yourArray, function ($v) use (&$temp) {
    if (in_array($v['time_start'], $temp)) {
        return false;
    } else {
        array_push($temp, $v['time_start']);
        return true;
    }
});

使用 array_filter() 来过滤基于回调结果的数组(我使用了一个从 PHP 5.3 开始就可以使用的匿名函数)。 time_start 值被收集到一个临时数组中。

$temp = array();
array_filter($yourArray, function ($v) use (&$temp) {
    if (in_array($v['time_start'], $temp)) {
        return false;
    } else {
        array_push($temp, $v['time_start']);
        return true;
    }
});

Uses array_filter() which will filter an array based on the result of a callback (I used an anonymous function which can be used since PHP 5.3). The time_start values are collected into a temporary array.

子栖 2024-11-15 06:37:22

我想你只需要步行:

$usedVals = array();
$outArray = array();
foreach ($targetArray as $arrayItem)
{
    if (!in_array($arrayItem['time_start'],$usedVals))
    {
        $outArray[] = $arrayItem;
        $usedVals[] = $arrayItem['time_start'];
    }
}
return $outArray;

I think you'll just have to walk it:

$usedVals = array();
$outArray = array();
foreach ($targetArray as $arrayItem)
{
    if (!in_array($arrayItem['time_start'],$usedVals))
    {
        $outArray[] = $arrayItem;
        $usedVals[] = $arrayItem['time_start'];
    }
}
return $outArray;
魂牵梦绕锁你心扉 2024-11-15 06:37:22
$uniq = array();
foreach($no_unique as $k=>$v) if(!isset($uniq[$v['time_start']])) $uniq[$v['time_start']] = $v;
$uniq = array_values($uniq);
$uniq = array();
foreach($no_unique as $k=>$v) if(!isset($uniq[$v['time_start']])) $uniq[$v['time_start']] = $v;
$uniq = array_values($uniq);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文