Learn more about how to delete elements from PHP arrays _ PHP Tutorial

Source: Internet
Author: User
Learn more about how to delete elements from PHP arrays. When learning PHP, you may encounter the PHP array deletion problem. here we will introduce the solution to the PHP array deletion problem, and share it with you here. If you have learned the C language, you may encounter the PHP array deletion problem. here we will introduce the solution to the PHP array deletion problem, and share it with you here. Anyone who has learned the C language may know that the C language is very powerful in string processing. PHP is written in C. Naturally, it inherits the advantage of C in string processing. After all, PHP is a new language, which is different from the C language. Naturally, PHP cannot be exactly the same as C. Therefore, some functions can only be known after testing. Sometimes each character of a string needs to be processed. the general practice may be:

 
 
  1. $Str="Something";
  2. For ($I=0; $ I<Strlen($ Str); $ I ++)
  3. {
  4. $Ch=Substr($ Str, $ I, 1 );
  5. // Process $ ch
  6. }

This is acceptable, but is there a more elegant way? Yes, it is treated as an array, and the C language is like this.

Let's change the above example to a string array processing method:

 
 
  1. $Str="Something";
  2. For ($I=0; $ I<Strlen($ Str); $ I ++)
  3. {
  4. $Ch= $ Str [$ I];
  5. // Process $ ch
  6. }

This is not much better.

Delete elements from PHP arrays

Define an array. what if I want to delete several items in the PHP array? I saw an answer in www.phpbuilder.com, that is, using the unset () function. Let's do a test.

 
 
  1. $a[]="a1";
  2. $a[]="a2";
  3. $a[]="a3";
  4. for($i=0; $i<sizeof($a); $i++)
  5. {
  6. echo $a[$i] . "
  7. ";
  8. }
  9. unset($a[0]);
  10. for($i=0; $i<sizeof($a); $i++)
  11. {
  12. echo $a[$i] . "
  13. ";
  14. }

What does that mean? A $ a array with three elements is displayed. then, 1st elements (subscript: 0) are deleted and displayed. The result should be two elements left in the array. But no! The answer is different from what we think. isn't unset () difficult to use? Think about it. The original unset ($ a [0]) deleted 1st elements, but we still started from $ I = 0 in the output. of course, this is incorrect, php does not automatically adjust the lower mark. In this way, we have to deal with it in other ways:

 
 
  1. $ A [] = "a1 ";
  2. $ A [] = "a2 ";
  3. $ A [] = "a3 ";
  4. For ($I=0; $ I<Sizeof($ A); $ I ++)
  5. {
  6. Echo $ a [$ I]."
  7. ";
  8. }
  9. Unset ($ a [0]);
  10. Reset ($ a); // returns the array pointer to 1st elements.
  11. While (list ($ c, $ d) = each ($ ))
  12. {
  13. Echo $ d ."
  14. "; // $ C is an array subscript
  15. }

This is a common method for displaying arrays. you do not need to consider the subscript of arrays.


Bytes. Anyone who has learned C language may know it...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.