php unset 数组 下标不错位 array_filter 直接在原数组上操作 filter myfilter

* Arr.php

class Arr {
    public static function some($a, callable $c) {
        $n = count($a);
        for ($i = 0; $i < $n; $i++) {
            if (call_user_func($c, $a[$i])) {
                return true;
            }
        }
        return false;
    }

    /**
     * 在原数组上过滤 不需要把返回值=重新赋值 也不需要array_values更新index
     * @param array $a
     * @param callable $c
     */
    public static function filter(array &$a, callable $c) {
        for ($i = 0; $i < count($a);) {
            if (call_user_func($c, $a[$i]) ) {
                $i++;
            } else {
                self::myUnset($a, $i);
            }
        }
    }

    private static function myUnset(array& $arr, $i) {
        $n = count($arr);
        if ($i<0 || $i >= $n) {
            return;
        }
        for ($j = $i+1; $j < $n; $i++, $j++) {
            $arr[$i] = $arr[$j];
        }
        unset($arr[$i]);
    }

}

替换系统的array_filter函数

 

* myunset.php

= count($arr)) {
		return;
	}
	for ($j = $i+1; $j < count($arr); $i++, $j++) {
		$arr[$i] = $arr[$j];
	}
	unset($arr[$i]);
}

// test

$a = str_split("UNSET");
myunset($a, 0);
// unset($a[0]);
var_dump($a);

 

php myunset.php
D:\test\myunset.php:16:
array(4) {
  [0] =>
  string(1) "U"
  [1] =>
  string(1) "N"
  [2] =>
  string(1) "S"
  [3] =>
  string(1) "E"
}

 

如果调用系统的 unset($a[0]);

下标为0的键值对丢失, 调用json_encode,导致类型转换, 数组转换为对象, 意想不到的错误

>php myunset.php
D:\test\myunset.php:16:
array(4) {
  [1] =>
  string(1) "N"
  [2] =>
  string(1) "S"
  [3] =>
  string(1) "E"
  [4] =>
  string(1) "T"
}

 

http://php.net/manual/en/function.array-filter.php

array_filter返回一个新的数组,如果要在原数组上操作 myfilter.php

// 仅试用与索引数组array, 不能是map, Object

 count($arr)) {
        return;
    }
    for ($j = $i+1; $j < count($arr); $i++, $j++) {
        $arr[$i] = $arr[$j];
    }
    unset($arr[$i]);
}

function myfilter(array &$a, callable $c) {
    for ($i = 0; $i < count($a);) {
        if (call_user_func($c, $a[$i]) ) {
            $i++;
        } else {
            myunset($a, $i);  
        }
    }
}

// test:

$s = '[{"id":2273,"user_id":27952046,"course_guid":"P33950","session_id":"0ce4d8f2b3dc09e9b01ecdb6f1316f9b","insert_time":"2018-05-19 16:38:28","remarks":"{\"isnewpolicyarea\":0,\"ischoice\":0,\"province\":\"\\u5317\\u4eac\",\"activemethod\":\"00\"}","source":3},{"id":2274,"user_id":27952046,"course_guid":"P33877","session_id":"0ce4d8f2b3dc09e9b01ecdb6f1316f9b","insert_time":"2018-05-19 17:00:56","remarks":"{\"isnewpolicyarea\":0,\"ischoice\":0,\"province\":\"\",\"activemethod\":\"00\"}","source":1},{"id":2275,"user_id":27952046,"course_guid":"P40355","session_id":"0ce4d8f2b3dc09e9b01ecdb6f1316f9b","insert_time":"2018-05-19 17:37:20","remarks":"{\"isnewpolicyarea\":0,\"ischoice\":0,\"province\":\"\\u8d35\\u5dde\",\"activemethod\":\"00\"}","source":1},{"id":2283,"user_id":29355088,"course_guid":"P39556","session_id":"u9kdf9je2tb1hdht19oalpbfl4","insert_time":"2018-10-25 08:38:42","remarks":"{\"isnewpolicyarea\":0,\"ischoice\":0,\"province\":\"\\u6e56\\u5317\",\"activemethod\":\"00\"}","source":1}]';
$a = json_decode($s);
myfilter($a, function($o) {
    return $o->source === 3;
});

// echo '
';
var_dump($a);

$array1 = array("a"=>1, "b"=>2, "c"=>3, "d"=>4, "e"=>5);
$array2 = array(6, 7, 8, 9, 10, 11, 12);

var_dump($array1[0]);
//myfilter($array1, function($var) {
//    return ($var & 1);
//});
//print_r($array1);

myfilter($array2, function($var) {
    return ! ($var & 1);
});
print_r($array2);

 

D:\test>php filter.php

array(1) {

  [0]=>

  object(stdClass)#1 (7) {

    ["id"]=>

    int(2273)

    ["user_id"]=>

    int(27952046)

    ["course_guid"]=>

    string(6) "P33950"

    ["session_id"]=>

    string(32) "0ce4d8f2b3dc09e9b01ecdb6f1316f9b"

    ["insert_time"]=>

    string(19) "2018-05-19 16:38:28"

    ["remarks"]=>

    string(74) "{"isnewpolicyarea":0,"ischoice":0,"province":"北京","activemethod":"00"}"

    ["source"]=>

    int(3)

  }

}

NULL

Array

(

    [0] => 6

    [1] => 8

    [2] => 10

    [3] => 12

)

开发环境要打开所有错误信息:

error_reporting(E_ALL);
ini_set('display_errors', 1);

?>

 

 

你可能感兴趣的:(php)