String array, delete array elements

Source: Internet
Author: User
Anyone who has learned C language from string arrays may know that C language is very powerful in string processing, and 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. So some SyntaxHighlighter. all ();

String array
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:

$ Str = "something ";
For ($ I = 0; $ I {
$ Ch = substr ($ str, $ I, 1 );
// Process $ ch
}

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:

$ Str = "something ";
For ($ I = 0; $ I {
$ Ch = $ str [$ I];
// Process $ ch
}

This is not much better.

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

$ A [] = "a1 ";
$ A [] = "a2 ";
$ A [] = "a3 ";
For ($ I = 0; $ I {
Echo $ a [$ I]."
";
}
Unset ($ a [0]);
For ($ I = 0; $ I {
Echo $ a [$ I]."
";
}

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:



$ A [] = "a1 ";
$ A [] = "a2 ";
$ A [] = "a3 ";
For ($ I = 0; $ I {
Echo $ a [$ I]."
";
}
Unset ($ a [0]);
Reset ($ a); // returns the array pointer to 1st elements.
While (list ($ c, $ d) = each ($ ))
{
Echo $ d ."
"; // $ C is an array subscript
}

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

Note: sizeof () is used to return the number of arrays, the same as count.

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.