Simple case Study of PHP array operation

Source: Internet
Author: User
This article is an example of how PHP array manipulation techniques are concerned. Share to everyone for your reference, as follows:

This is a simple PHP array entry question.

$STR = "AS5454654%^$%^$7675DHASJKDHH12U123123ASDASD";//will be above the statistics above the string different characters and occurrences of the number.

Implementation: Converts a string into an array, resulting in a corresponding result by an array of operations.

$len = strlen ($STR);//array exists in array $array = Array (); for ($i =0; $i < $len; $i + +) {  Array_push ($array, substr ($str, $i, 1));}

Based on the conversion function above, we get the result we want.

The next three ways to complete the above.

The sort () method 1//an array $arr = sort ($array);//Remove duplicate values $arr1 = Array_unique ($array);//print Arrays $ARR1 you will find that you continue to do what is needed. Note the direct relationship of the key value

Here's a look at method two:

The most coquettish way two $arr2 = Array_count_values ($array);//print array $arr2 All the problems are solved.

is not think method two very pit Father Ah ~

Note: All can use the PHP automatic function, the best is to use, the reason I do not say, you should also understand

Method Three:

I'm only talking about thinking here: Using the ACM idea, the element array is quickly ranked, and then the arrays are divided into blocks.

Look at this function to give you the above hint:

Array Array_splice (array $input, int $offset [, int $length < [, array $replacement]])//Convert input array by offset and lengt h The specified unit is removed, if the replacement parameter is provided,//is replaced with a cell in the replacement array. Returns an array containing the cells that have been removed. Note the numeric key names in input are not retained.

There are a lot of ways to solve the problem, I now think of these three kinds, a simple question, think more. The code is going to knock less and more. This is progress.

I hope this article is helpful to you in PHP programming.

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.