PHP Array operation simple case analysis _php tips

Source: Internet
Author: User

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.

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.