Simple case analysis of PHP Array Operations, array Case Analysis

Source: Internet
Author: User

Simple case analysis of PHP Array Operations, array Case Analysis

This example describes PHP Array Operations. We will share this with you for your reference. The details are as follows:

This is a simple PHP array entry question.

$ Str = "as5454654% ^ $ 7675dhasjkdhh12u123123asdasd"; // count the different characters and occurrences of the preceding string.

Implementation Method: Convert the string into an array and obtain the corresponding result through operations on the array.

$ Len = strlen ($ str); // The array contains $ array = array (); for ($ I = 0; $ I <$ len; $ I ++) {array_push ($ array, substr ($ str, $ I, 1 ));}

According to the above conversion function, we get the expected result.

Next, we will use three methods to complete the above.

// Method 1 // sort the array by sort () $ arr = sort ($ array); // remove the duplicate value $ arr1 = array_unique ($ array ); // print the array $ arr1 and you will find that you are continuing to do things. Note the direct relationship between key values

Let's take a look at Method 2:

// Method 2 $ arr2 = array_count_values ($ array); // print the array $ arr2. All problems are solved.

I don't think method 2 is very boring ~

Note:It is best to use PHP automatic functions for any reason.

Method 3:

I will only talk about the idea here: use ACM to sort the element arrays quickly and then process the arrays in blocks.

Let's take a look at this function and give you the above prompt:

Array array_splice (array $ input, int $ offset [, int $ length <[, array $ replacement]) // removes the units specified by offset and length in the input array, if the replacement parameter is provided, // replace it with cells in the replacement array. Returns an array containing the removed cells. Note that the number key name in input is not retained.

There are many ways to solve the problem. Now I want to think about these three types of problems, a simple problem. The less the code is. This is progress.

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.