Simple case analysis of PHP Array Operations

Source: Internet
Author: User
This article mainly introduces PHP array operations, and analyzes php array conversion, sorting, removal, and other related operation skills in the form of simple instances, for more information about PHP array operations, see the following example. 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.

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.

I hope this article will help you with PHP programming.

For more articles on simple case analysis of PHP array operations, refer to the PHP Chinese website!

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.