将php数组中的所有值与其他值进行比较

编程入门 行业动态 更新时间:2024-10-24 07:26:14
本文介绍了将php数组中的所有值与其他值进行比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个php数组

如何比较此数组的所有值并根据自定义逻辑(可能是回调函数)过滤掉值.

How can I compare all values of this array and filter out values based on custom logic (callback function maybe).

本质上,我想将每个数组值与数组中的每个其他值进行比较,并基于一些自定义逻辑,保留该值或将其从数组中删除

Essentially, I want to compare each array value with every other value within the array and based on some custom logic, either keep the value or remove it from the array

谢谢

推荐答案

可能您必须手动进行:

function your_callback($a, $b) { return $a != $b; } $array = array(/** Your array here... **/); $n = count($array); $filtered = array(); for($i = 0; $i < $n; $i++) { $ok = true; for($j = 0; $j < $n; $j++) { if($j != $i && !your_callback($array[$i], $array[$j]) { $ok = false; break; } } if($ok) array_push($filtered, $array[$i]); } unset($array); $array = $filtered;

例如,此示例将过滤数组的唯一值;例如:更改your_callback定义以实现其他逻辑.

This example will filter unique values of array for example; change your_callback definition to implement other logic.

更多推荐

将php数组中的所有值与其他值进行比较

本文发布于:2023-10-14 12:01:50,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1491006.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:组中   与其他   php

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!