Array Pointer in PHP

Dustin Jia

I'm a bit confused about array pointers in PHP. The below code worked fine:

$ages = [1, 3, 5];
while($age = current($ages)) {
  echo $age . ", ";
  next($ages);
}

But I've no idea why the below code didn't print any thing out:

$ages = [];
for($i = 0; $i < 10; $i++) {
  $ages[] = $i;
}
while($age = current($ages)) {
  echo $age . ", ";
  next($ages);
}

I also tried to print with for loop, but in below code only the for loop printed, the while loop still didn't print.

$ages = [];
for($i = 0; $i < 10; $i++) {
  $ages[] = $i;
}
for($i = 0; $i < 10; $i++) {
  echo $ages[$i] . ", ";
}
while($age = current($ages)) {
  echo $age . ", ";
  next($ages);
}

I'm really not sure why it behaved like this, anyone could help me out?

Qirel

You will need to check if the result of current() is different from the boolean false (means the cursor did not find the element), not just assign its value. As when the value is 0, you get while(0), which breaks the loop.

$ages = [];
for($i = 0; $i < 10; $i++) {
  $ages[] = $i;
}
while($age = current($ages) !== false) {
  echo $age . ", ";
  next($ages);
}

https://3v4l.org/61WoL

However, this will fail should any of the elements in the array have the value of boolean false. Its therefor not recommended to iterate over the array like this at all, you should instead be using the proper tools, by using a foreach loop. This doesn't actually move the cursor, but you can "make" it move the cursor by calling next() for each iteration.

$ages = [];
for($i = 0; $i < 10; $i++) {
  $ages[] = $i;
}
foreach ($ages as $age) {
    echo current($ages).", ";
    next($ages);
}

If you're just looking to print the values, the best way would be either printing directly from the foreach loop, or by using implode().

foreach ($ages as $age) {
    echo $age.", ";
}

or

echo impolode(",", $ages);

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

pointer to const array of ints

분류에서Dev

pointer to next element of an array

분류에서Dev

bsearch with array of pointer of structs

분류에서Dev

Pointer to array of struct in function

분류에서Dev

pointer to dynamic array of pointers to dynamic array of pointer that points to strings

분류에서Dev

Initializing a pointer to an array of integers.

분류에서Dev

C *char pointer to char array

분류에서Dev

Pointer arithmetic with multidimensional Array Notation

분류에서Dev

Allocate a new bool array pointer

분류에서Dev

Difference between an array and a pointer to an array once compiled?

분류에서Dev

Swapping 2d array (pointer to pointer) in C

분류에서Dev

Printing content of 2D array (pointer of pointer)

분류에서Dev

Accessing a triple pointer inside a function with array notation

분류에서Dev

Pointer Array Sorting Algorithm in C++

분류에서Dev

Difference between array and pointer versions of external declarations

분류에서Dev

Passing Array of Structs to a Function as a Pointer (C)

분류에서Dev

accessing the member of a class of pointer array of another class

분류에서Dev

Passing a pointer to a char array as an argument to a function - C

분류에서Dev

Why the size of pointer to array is always 8 in C?

분류에서Dev

C++ copying array into pointer argument

분류에서Dev

How to incress position of a pointer that is pointing to a char array?

분류에서Dev

Multidimensional array to array in PHP

분류에서Dev

Copy pointer array values to end of another array in C

분류에서Dev

PHP array in array - DXF script

분류에서Dev

Merge a php array and a php object

분류에서Dev

Json encode array with php - array() becomes "Array{...}" not "[...]"

분류에서Dev

accessing double pointer 2d array passed by reference to a function

분류에서Dev

Getting the address of a statically allocated array into a pointer inside a struct

분류에서Dev

how to dereference index of pointer array of structures within a structure

Related 관련 기사

  1. 1

    pointer to const array of ints

  2. 2

    pointer to next element of an array

  3. 3

    bsearch with array of pointer of structs

  4. 4

    Pointer to array of struct in function

  5. 5

    pointer to dynamic array of pointers to dynamic array of pointer that points to strings

  6. 6

    Initializing a pointer to an array of integers.

  7. 7

    C *char pointer to char array

  8. 8

    Pointer arithmetic with multidimensional Array Notation

  9. 9

    Allocate a new bool array pointer

  10. 10

    Difference between an array and a pointer to an array once compiled?

  11. 11

    Swapping 2d array (pointer to pointer) in C

  12. 12

    Printing content of 2D array (pointer of pointer)

  13. 13

    Accessing a triple pointer inside a function with array notation

  14. 14

    Pointer Array Sorting Algorithm in C++

  15. 15

    Difference between array and pointer versions of external declarations

  16. 16

    Passing Array of Structs to a Function as a Pointer (C)

  17. 17

    accessing the member of a class of pointer array of another class

  18. 18

    Passing a pointer to a char array as an argument to a function - C

  19. 19

    Why the size of pointer to array is always 8 in C?

  20. 20

    C++ copying array into pointer argument

  21. 21

    How to incress position of a pointer that is pointing to a char array?

  22. 22

    Multidimensional array to array in PHP

  23. 23

    Copy pointer array values to end of another array in C

  24. 24

    PHP array in array - DXF script

  25. 25

    Merge a php array and a php object

  26. 26

    Json encode array with php - array() becomes "Array{...}" not "[...]"

  27. 27

    accessing double pointer 2d array passed by reference to a function

  28. 28

    Getting the address of a statically allocated array into a pointer inside a struct

  29. 29

    how to dereference index of pointer array of structures within a structure

뜨겁다태그

보관