php - 특정 - strip_tags 함수
이 비추천 함수를 해결하는 방법은 각 PHP (2)
PHP 7.2에서는 each
함수가 사용되지 않습니다. 문서에 의하면 :
경고이 함수는 PHP 7.2.0부터 사용이 권장되지 않습니다. 이 기능에 의존하는 것은 매우 바람직하지 않습니다.
코드를 사용하지 않으려면 어떻게 업데이트합니까? 여기 예시들이 있습니다 :
$ar = $o->me; reset($ar); list($typ, $val) = each($ar);
$out = array('me' => array(), 'mytype' => 2, '_php_class' => null); $expected = each($out);
for(reset($broken);$kv = each($broken);) {...}
list(, $this->result) = each($this->cache_data);
// iterating to the end of an array or a limit > the length of the array $i = 0; reset($array); while( (list($id, $item) = each($array)) || $i < 30 ) { // code $i++; }
당신은 key() , current() , next() 사용하여 자신 만의 each()
함수를 만들 수 있습니다. 다음과 같이 해당 함수로 호출을 바꿉니다.
<?php
function myEach(&$arr) {
$key = key($arr);
$result = ($key === null) ? false : [$key, current($arr), 'key' => $key, 'value' => current($arr)];
next($arr);
return $result;
}
1.
$ar = $o->me;
reset($ar);
list($typ, $val) = myEach($ar);
2.
$out = array('me' => array(), 'mytype' => 2, '_php_class' => null);
$expected = myEach($out);
삼.
for(reset($broken);$kv = myEach($broken);) {...}
첫 번째 두 가지 사례의 경우 key
와 current
를 사용하여 필요한 값을 할당 할 수 있습니다.
$ar = $o->me; // reset isn't necessary, since you just created the array $typ = key($ar); $val = current($ar);
$out = array('me' => array(), 'mytype' => 2, '_php_class' => null); $expected = [key($out), current($out)];
이 경우, next
를 사용하여 커서를 앞으로 이동시킬 수는 있지만, 나머지 코드가 그것에 의존하지 않는다면 필요하지 않을 수도 있습니다.
세 번째 경우에는 대신 foreach 루프를 사용하고 루프 내부에 $kv
할당하는 것이 좋습니다.
foreach ($broken as $k => $v) {
$kv = [$k, $v];
}
네 번째 경우에는 키가 list
에서 무시되는 것처럼 보이므로 현재 값을 지정할 수 있습니다.
$this->result = current($this->cache_data);
처음 두 경우와 마찬가지로, 나머지 코드가 $this->cache_data
와 상호 작용하는 방식에 따라 next
커서를 이동시켜야 할 수도 있습니다.
# 5는 for 루프로 대체 될 수 있습니다.
reset($array);
for ($i = 0; $i < 30; $i++) {
$id = key($array);
$item = current($array);
// code
next($array);
}