array(4) { [0] => array(11) { ["id"] => string(1) "1",["k"] => string(1) "Q"">
当前位置:Gxlcms > PHP教程 > php数组过滤问题?

php数组过滤问题?

时间:2021-07-01 10:21:17 帮助过:4人阅读

怎么把类似这样的数组过

["Q"] => array(4) {
        [0] => array(11) {
          ["id"] => string(1) "1",["k"] => string(1) "Q"
        }
         [1] => array(11) {
          ["id"] => string(1) "2",["k"] => string(1) "H"
        }
        [2] => array(11) {
          ["id"] => string(1) "3", ["k"] => string(1) "V"
        }
         [3] => array(11) {
          ["id"] => string(1) "4",  ["k"] => string(1) "V"
        }
}
["V"] => array(4) {
        [0] => array(11) {
          ["id"] => string(1) "1",["k"] => string(1) "Q"
        }
         [1] => array(11) {
          ["id"] => string(1) "2",["k"] => string(1) "H"
        }
        [2] => array(11) {
          ["id"] => string(1) "3", ["k"] => string(1) "V"
        }
         [3] => array(11) {
          ["id"] => string(1) "4",  ["k"] => string(1) "V"
        }
}
["H"] => array(4) {
        [0] => array(11) {
          ["id"] => string(1) "1",["k"] => string(1) "Q"
        }
         [1] => array(11) {
          ["id"] => string(1) "2",["k"] => string(1) "H"
        }
        [2] => array(11) {
          ["id"] => string(1) "3", ["k"] => string(1) "V"
        }
         [3] => array(11) {
          ["id"] => string(1) "4",  ["k"] => string(1) "V"
        }
}

滤成这样

["Q"] => array(1) {
        [0] => array(11) {
          ["id"] => string(1) "1"
          ["k"] => string(1) "Q"
        }
}
["V"] => array(2) {
        [0] => array(11) {
          ["id"] => string(1) "3"
          ["k"] => string(1) "V"
        }
         [1] => array(11) {
          ["id"] => string(1) "4"
          ["k"] => string(1) "V"
        }
}
["H"] => array(1) {
         [0] => array(11) {
          ["id"] => string(1) "2"
          ["k"] => string(1) "H"
        }
}

回复内容:

怎么把类似这样的数组过

["Q"] => array(4) {
        [0] => array(11) {
          ["id"] => string(1) "1",["k"] => string(1) "Q"
        }
         [1] => array(11) {
          ["id"] => string(1) "2",["k"] => string(1) "H"
        }
        [2] => array(11) {
          ["id"] => string(1) "3", ["k"] => string(1) "V"
        }
         [3] => array(11) {
          ["id"] => string(1) "4",  ["k"] => string(1) "V"
        }
}
["V"] => array(4) {
        [0] => array(11) {
          ["id"] => string(1) "1",["k"] => string(1) "Q"
        }
         [1] => array(11) {
          ["id"] => string(1) "2",["k"] => string(1) "H"
        }
        [2] => array(11) {
          ["id"] => string(1) "3", ["k"] => string(1) "V"
        }
         [3] => array(11) {
          ["id"] => string(1) "4",  ["k"] => string(1) "V"
        }
}
["H"] => array(4) {
        [0] => array(11) {
          ["id"] => string(1) "1",["k"] => string(1) "Q"
        }
         [1] => array(11) {
          ["id"] => string(1) "2",["k"] => string(1) "H"
        }
        [2] => array(11) {
          ["id"] => string(1) "3", ["k"] => string(1) "V"
        }
         [3] => array(11) {
          ["id"] => string(1) "4",  ["k"] => string(1) "V"
        }
}

滤成这样

["Q"] => array(1) {
        [0] => array(11) {
          ["id"] => string(1) "1"
          ["k"] => string(1) "Q"
        }
}
["V"] => array(2) {
        [0] => array(11) {
          ["id"] => string(1) "3"
          ["k"] => string(1) "V"
        }
         [1] => array(11) {
          ["id"] => string(1) "4"
          ["k"] => string(1) "V"
        }
}
["H"] => array(1) {
         [0] => array(11) {
          ["id"] => string(1) "2"
          ["k"] => string(1) "H"
        }
}

array(
        array(
            'id'=>1,
            'k'=>'Q'
        ),
        array(
            'id'=>2,
            'k'=>'H'
        ),
        array(
            'id'=>3,
            'k'=>'V'
        ),
        array(
            'id'=>4,
            'k'=>'V'
        ),                    
    ),    
    'V'=>array(
        array(
            'id'=>1,
            'k'=>'Q'
        ),
        array(
            'id'=>2,
            'k'=>'H'
        ),
        array(
            'id'=>3,
            'k'=>'V'
        ),
        array(
            'id'=>4,
            'k'=>'V'
        ),    
    ),
    'H'=>array(
        array(
            'id'=>1,
            'k'=>'Q'
        ),
        array(
            'id'=>2,
            'k'=>'H'
        ),
        array(
            'id'=>3,
            'k'=>'V'
        ),
        array(
            'id'=>4,
            'k'=>'V'
        ),    
    ),    
);

echo '
';
print_r($arr);

$new_arr = array();
foreach($arr as $key=>$val) {
    foreach($val as $k=>$v) {
        if($key == $v['k']) {
            $new_arr[$key][] = $v;
        }
    }
}
echo '
'; print_r($new_arr);

array_filter($arr,fun);
fun里面用数组的key去和k判断,不等于的unset掉


$keys = array_keys($arr);
$newArr = array();
$i = 0;
foreach ($arr as $key => $value) {
    $newArr[$key] = array_filter($value, function ($val) {
        GLOBAL $keys;
        GLOBAL $i;
        return $val["k"] == $keys[$i];
    }); 
    $i++;
}

人气教程排行