欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

php数组处置

程序员文章站 2022-06-07 13:01:16
...
php数组处理
array(4) {
[0] => array(3) {
["Id"] => string(2) "24"
["FromMid"] => string(3) "157"
["ToMid"] => string(3) "157"
}
[1] => array(3) {
["Id"] => string(2) "22"
["FromMid"] => string(3) "157"
["ToMid"] => string(3) "176"
}
[2] => array(3) {
["Id"] => string(1) "4"
["FromMid"] => string(3) "157"
["ToMid"] => string(2) "25"
}
[3] => array(3) {
["Id"] => string(1) "1"
["FromMid"] => string(2) "25"
["ToMid"] => string(3) "157"
}
}

我想把["FromMid"]==["ToMid"]&&["ToMid"]==["FromMid"]处理掉 只留一条
得出的结果应该是
array(4) {
[0] => array(3) {
["Id"] => string(2) "24"
["FromMid"] => string(3) "157"
["ToMid"] => string(3) "157"
}
[1] => array(3) {
["Id"] => string(2) "22"
["FromMid"] => string(3) "157"
["ToMid"] => string(3) "176"
}
[2] => array(3) {
["Id"] => string(1) "4"
["FromMid"] => string(3) "157"
["ToMid"] => string(2) "25"
}
}

------解决方案--------------------
PHP code


------解决方案--------------------
PHP code

$data = array(
    array("Id" => "24", "FromMid" => "157", "ToMid" => "157"),
    array("Id" => "22", "FromMid" => "157", "ToMid" => "176"),
    array("Id" => "4", "FromMid" => "157", "ToMid" => "25"),
    array("Id" => "1", "FromMid" => "25", "ToMid" => "157")
);

$markList = array();
foreach ($data as $key => $val) {
    $posi = md5($val['FromMid'] . $val['ToMid']);
    $anti = md5($val['ToMid'] . $val['FromMid']);
    if (isset($markList[$posi]) || isset($markList[$anti])) {
        unset($data[$key]);
    } else {
        $markList[$posi] = $markList[$anti] = 1;
    }
}
echo '
';print_r($data);

/**
输出:
Array
(
    [0] => Array
        (
            [Id] => 24
            [FromMid] => 157
            [ToMid] => 157
        )

    [1] => Array
        (
            [Id] => 22
            [FromMid] => 157
            [ToMid] => 176
        )

    [2] => Array
        (
            [Id] => 4
            [FromMid] => 157
            [ToMid] => 25
        )
*/
 
php数组处置

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。

相关文章

相关视频