search
HomeBackend DevelopmentPHP TutorialBriefly understand the use of array pointers in PHP programming_php skills

To use the elements in the array, you need the positioning of the array. The implementation of positioning needs to be completed with the help of array pointers. There are many functions in PHP to move array pointers. I will introduce a few to you below.

Move the pointer to the next array position next()

The next() function returns the array value placed immediately next to the current array pointer. Its form is as follows:

mixed next(array array)

Here is an example:

$fruits = array("apple", "banana", "orange", "pear");

echo next($fruits);

echo next($fruits);

// banana

// orange

You can also move the pointer forward, or directly move it to the beginning and end of the array.

Move the pointer to the previous array position prev()

The prev() function returns the array value located at the previous position of the current pointer. If the pointer is originally located at the first position of the array, it returns false. Its form is as follows:

mixed prev(array array)

The usage of prev() is the same as next(), so the example is omitted.

Move the pointer to the first array position reset()

The reset() function is used to set the array pointer back to the beginning of the array. Its form is as follows:

mixed reset(array array)

This function is often used if you need to view or process an array multiple times in a script. In addition, this function is often used at the end of sorting.

Move the pointer to the last array position end()

The

end() function moves the pointer to the last position of the array and returns the last element. Its form is as follows:

mixed end(array array)

The following example shows how to get the first and last array value:

$fruits = array("apple", "banana", "orange", "pear");

echo current($fruits);

echo end($fruits);

// apple

// pear

There is such a rule when arrays are passed as parameters between functions: we know that when a function is called, the system will copy a copy of the actual parameters to the formal parameters (except for reference calls), but for arrays, not only copy The value of the actual parameter is obtained, and the position of the current internal pointer of the actual parameter group is also copied. If the position of the internal pointer of the actual parameter points to the end of the array, the system will reset the internal pointer of the formal parameter to point to the first unit of the formal parameter group; if the position of the internal pointer of the actual parameter is not at the end of the array, it points to a valid unit, then the system will point the array pointer position of the formal parameter to the array unit with the same value as the array pointer of the actual parameter.

If you do not do $arr['var6'] = 6, all 6 variables ($var1-$var6) will have values, because after each, the array pointer has pointed to the end of the array, then after When the function func() is called, the system resets the array pointer of $arrtmp to point to the first element. But after the operation $arr['var6'] = 6, everything changed. This operation changed the array pointer of $arr from pointing to a NULL to a valid value (explain, before and after the assignment, the array The address unit pointed by the pointer has not changed. It is just that before the assignment, there was nothing in that address unit, but after the assignment, it became 6). This makes the array pointer of $arr point to a valid unit. When calling function func(), the system will not reset the array pointer of $arrtmp. The array pointer of $arrtmp will be the same as the array pointer of $arr. Same, pointing to his own last unit. The each function starts working from the position of the current array pointer. Therefore, the return value of the first result of the each function operation is the last element of the array $arrtmp, which moves the array pointer down one more bit. The while loop ends here, so $arrtmp['var1']-$arrtmp ['var5'] has not been traversed, eventually causing $var1-$var6 to be NULL.

In the process of array assignment, the changes in the array pointers of the assigning array and the assigned array. First give a conclusion, and then we use code to prove this conclusion. $arrtmp=$arr; In this assignment expression, I call $arr the assignment array, and $arrtmp the assigned array. When the array is assigned, if the array pointer of the assigned array already points to the end of the array, the array pointer of the assigned array will be reset after the assignment and points to the first element of the array; if during the assignment, the array pointer of the assigned array does not point to the array. At the end, it points to any valid array element. Then the array pointer of the assigned array will not be reset after the assignment, but the element it originally pointed to will be retained. After the assignment, the assigned array not only has the value of the assigned array, but also the array pointer of the assigned array points to that element, and the assigned array will also point to the element with the same value in itself.

demo1:

<&#63;php
 $arr = array('var1'=>1,'var2'=>2,'var3'=>3,'var4'=>4,'var5'=>5);
 while( list($key,$value) = each($arr) )
 {
 if($value == 4) break;
 }
 var_dump(current($arr));
 
 $arr1 = $arr;
 
 var_dump(current($arr));
 var_dump(current($arr1));
&#63;>

demo1 的执行结果是:int(5) int(5) int(5) 。从这个结果可以看出,赋值前后$arr的数组指针位置没有发生任何变化,$arr1不仅值跟$arr相同,而且数组指针所指向的元素值也是相同的。现在 用上述结论来解释这个结果,在while循环中,有一个if判断语句,目的是不让$arr的数组指针指向数组末尾,而是保留在一个有效的位置。 在$value=4时会跳出循环,而each这个函数会将数组指针向前移动一位,这就导致了$arr的数组指针指向了第5个元素,所以在赋值之 前,current($arr)的结果是5,赋值之后,由于在赋值之前$arr的当前指针并没有指向末尾,因此在赋值之后不会将$arr的数组指针进行重 置,而是保留了其原有的位置,因此在赋值之后使用current($arr)的结果仍然是5。赋值时$arr1不仅获得了$arr的值,而且数组指针指向 的元素和$arr的相同,二者都是5。

<&#63;php
$arr = array('var1'=>1,'var2'=>2,'var3'=>3,'var4'=>4,'var5'=>5);
while( list($key,$value) = each($arr) )
{
  //if($value == 4) break;
}
var_dump(current($arr));
$arr1 = $arr;
var_dump(current($arr));
var_dump(current($arr1));
&#63;>

demo2中我们将 if($value == 4) break; 这一句注释掉了,目的很简单,就是通过each将$arr的数组指针位置指向数组末尾。

demo2 的执行结果:bool(false) int(1) bool(false) 。如果数组指针对应的元素为0,"",或者不是一个有效的值时,current函数会返回false,$arr的值中没有为0或者""的情况,因此可以断 定是因为数组指针指向了一个无效的元素而导致current返回了一个false。换句话说就是可以确定在while循环完成之后,$arr的数组指针已 经指向了数组的末尾。所以我们看到在赋值之前current($arr)的值是false,而赋值之后current($arr)的值变成了1,说明赋值 之后$arr的数组指针被重置了,指向了数组的第一个元素。current($arr1)的值为false,说明赋值之后$arr1让然保留了赋值之 前$arr的数组指针指向的元素。

通过demo1和demo2就可以证明上述结论了。

因此为了在遍历数组时不受数组指针的影响,最好在使用each()函数之前或者之后调用函数reset()将数组指针重置。这样就可以避免上述问题的发生了。另外还有一个操作数组指针的函数prev(),它的作用是将数组指针当前的位置后退一位,它也需要注意一点,就是如果数组指针已经指向数组末尾,那么使它就得不到想要的结果了。

顺便说一下foreach这个函数,使用foreach函数来遍历数组时,它会重置数组指针,将其指向数组的第一个元素。必须注意的是foreach操作的对象是对你要遍历的数组的copy值,而不是遍历数组本身。

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
php怎么把负数转为正整数php怎么把负数转为正整数Apr 19, 2022 pm 08:59 PM

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

php怎么实现几秒后执行一个函数php怎么实现几秒后执行一个函数Apr 24, 2022 pm 01:12 PM

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php怎么除以100保留两位小数php怎么除以100保留两位小数Apr 22, 2022 pm 06:23 PM

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

php怎么根据年月日判断是一年的第几天php怎么根据年月日判断是一年的第几天Apr 22, 2022 pm 05:02 PM

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php怎么替换nbsp空格符php怎么替换nbsp空格符Apr 24, 2022 pm 02:55 PM

方法:1、用“str_replace("&nbsp;","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\&nbsp\;||\xc2\xa0)/","其他字符",$str)”语句。

php怎么判断有没有小数点php怎么判断有没有小数点Apr 20, 2022 pm 08:12 PM

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

php字符串有没有下标php字符串有没有下标Apr 24, 2022 am 11:49 AM

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

php怎么设置implode没有分隔符php怎么设置implode没有分隔符Apr 18, 2022 pm 05:39 PM

在PHP中,可以利用implode()函数的第一个参数来设置没有分隔符,该函数的第一个参数用于规定数组元素之间放置的内容,默认是空字符串,也可将第一个参数设置为空,语法为“implode(数组)”或者“implode("",数组)”。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment