Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/232.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP数组切片改进版_Php_Arrays_Slice - Fatal编程技术网

PHP数组切片改进版

PHP数组切片改进版,php,arrays,slice,Php,Arrays,Slice,我想知道是否有一种干净的方法可以根据位置条件对数组进行切片,这样(例如)每个第五个元素都将被删除,反之亦然(例如)只拾取第五个元素 <?php $input = array("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m"); print_r ( array_slice ( $input, 0, count($input) ) ); //works but not what I need. // This i

我想知道是否有一种干净的方法可以根据位置条件对数组进行切片,这样(例如)每个第五个元素都将被删除,反之亦然(例如)只拾取第五个元素

<?php
$input = array("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m");

print_r ( array_slice ( $input, 0, count($input) ) ); //works but not what I need.

// This is not the correct syntax but just to get the idea

// print_r ( array_slice ( $input, 0, count($input) ) , {ONLY IF POSITION IS NOT MOD 5 SO 5,10,15,20,25 ... (ETC) WILL BE DROPPED} );

// AND VICE VERSA

// print_r ( array_slice ( $input, 0, count($input) ) , {ONLY IF POSITION IS MOD 5 SO IT WILL SELECT POSITION 5,10,15,20,25 ... (ETC) } );

?>

如果没有用于该任务的专用php命令,则可以使用循环:)


谢谢

为什么不编写自己的版本

<?php
    $input = array(
        "a", "b", "c", "d", "e", "f", "g",
        "h", "i", "j", "k", "l", "m"
    );
    $removeElementAtInterval = 5;
    foreach($input as $key => $value) {
        if($key % $removeElementAtInterval == 0 && $key != 0) {
            unset($input[$key - 1]);
        }
    }
    print_r($input);
?>

使用以下功能:

function array_remove_nth($array, $nth)
{
    for($i = 0; $i < count($array); $i+=$nth)
        unset($array[$i]);

    return $array;
}

function array_select_nth($array, $nth)
{
    $returnArr = array();
    for($i = 0; $i < count($array); $i+=$nth)
        $returnArr[] = $array[$i];

    return $returnArr;
}

你需要什么格式?你能详细说明一下吗?谢谢大家!很好用!:)
<?php
    function array_filter_with_index(array $arr, $callback = null)
    {   
        if (!is_callable($callback)) {
            return $arr;
        }

        $result = array();

        for ($i = 0; $i < count($arr); $i++) {
            if ($callback($i, $arr[$i])) {
                $result[] = $arr[$i];
            }
        }

        return $result;
    }

    function mod_by_5($idx, $elt)
    {   
        return ($idx % 5) === 0;
    }

    function not_mod_by_5($idx, $elt)
    {   
        return ($idx % 5) !== 0;
    }

    $input = array("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m");

    var_dump(array_filter_with_index($input, 'mod_by_5'));
    var_dump(array_filter_with_index($input, 'not_mod_by_5'));

    // Or with anonymous functions:
    $result = array_filter_with_index($input, function($idx, $elt) {
        return $idx > 6;
    });

    var_dump($result);
?>
function array_remove_nth($array, $nth)
{
    for($i = 0; $i < count($array); $i+=$nth)
        unset($array[$i]);

    return $array;
}

function array_select_nth($array, $nth)
{
    $returnArr = array();
    for($i = 0; $i < count($array); $i+=$nth)
        $returnArr[] = $array[$i];

    return $returnArr;
}
$removedArr = array_remove_nth($yourArray, 5);
$selectedArr = array_select_nth($yourArray, 5);