The examples in this article describe the techniques associated with PHP array manipulation. Share to everyone for your reference, specific as follows:
This is a simple PHP array entry question
$STR = "AS5454654%^$%^$7675DHASJKDHH12U123123ASDASD";
The above statistics above the number of different characters and occurrences of the string.
Implementation: Converts a string into an array, and obtains the corresponding result by manipulating the array.
$len = strlen ($STR);
Array is present in the array
$array = Array ();
For ($i =0 $i < $len; $i + +)
{
Array_push ($array, substr ($str, $i, 1));
According to the transformation function above, we get the result that we want.
The next three ways to complete the above.
Method 1
//array for sort ()
$arr = sort ($array);
Remove duplicate value
$arr 1 = array_unique ($array);
Print array $arr1 You will find that you continue to do what needs to be done. Note the direct relationship between the key values
Effect chart
Let's take a look at method two:
The most coquettish method two
$arr 2 = array_count_values ($array);
Print array $arr2 All the problems are solved.
Effect chart
I don't think the method two is very pit
Note: You can use the PHP automatic function, the best is to use , the reason I do not say, you should also understand
Method Three:
I only say the thought: Using the ACM thought, the element array is arranged after the fast row, and then the log group is divided into block processing.
Effect chart
Look at this function to give you the above hint:
Array Array_splice (array $input, int $offset [, int $length < [, array $replacement]])
//Put the input array by offset and The cell specified by length is removed, if the replacement parameter is provided,
//The cell in the replacement array is substituted. Returns an array containing the removed cells. Note that the numeric key name in input is not preserved.
Many ways to solve the problem, I now think of these three kinds, a simple question, think more. The more the code is going to be knocking less. This is progress.
For more information about PHP interested readers can view the site topics: "PHP array Operation skills Encyclopedia", "PHP Sorting algorithm Summary", "PHP Basic Grammar Primer", "PHP Operations and Operator Usage Summary", "PHP object-oriented Programming Program Introduction", " PHP Network Programming skills Summary, "PHP string (String) Usage Summary", "Php+mysql Database operation Introduction Tutorial" and "PHP common database Operation Skills Summary"
I hope this article will help you with the PHP program design.