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.