PHP array processing methods, php Array Processing Methods

Source: Internet
Author: User

PHP array processing methods, php Array Processing Methods

1. Split the string into an array: $ array = explode (separator, $ string ):

Separator: separator,

$ String: The split string;

2. Combine the array into a string: $ string = implode (glue, $ array ):

Glue: glue combination,

$ Array: array;

3. Length of the statistical array: count ():

The count function has two parameters:
0 (or COUNT_NORMAL) is the default value. Multi-dimensional arrays (arrays in the array) are not detected );
1 (or COUNT_RECURSIVE) is a multi-dimensional array for Detection

<? Php
$ Arr = array (
0 => array ('title' => 'apple', 'viewnum' => green, 'content' => 'pingguo '),
1 => array ('title' => 'city', 'viewnum' => Red-hearted, 'content' => 'youzi ')
);

 

Echo 'multi-dimensional array not counted:'. count ($ arr, 0); // count ($ arr, COUNT_NORMAL)
Echo "<br/> ";
Echo 'multi-Dimensional Statistics array: '. count ($ arr, 1); // count ($ arr, COUNT_RECURSIVE)
?>

4. merge two Arrays: array_merge ():

$ A [];

$ B [];

$ C [] = array_merge ($ a, $ B );

5. array sorting:

  • Sort ()-sort arrays in ascending order
  • Rsort ()-Sort arrays in descending order
  • Asort ()-Sort joined arrays in ascending order by value
  • Ksort ()-Sort joined arrays in ascending order by key
  • Arsort ()-Sort associated arrays in descending order by value
  • Krsort ()-Sort associated arrays in descending order by key

The array_multisort () function sorts multiple arrays or multi-dimensional arrays.

<? Php

$ A1 = array (, 25 );

$ A2 = array (, 66 );

$ Num = array_merge ($ a1, $ a2 );

array_multisort($num,SORT_DESC,SORT_NUMERIC);

Print_r ($ num );

?>
6. array and json format conversion:
Json_encode (): converts an array to a json object format. append the parameter JSON_UNESCAPED_UNICODE to encode Chinese characters;

Json_decode (): converts a json object to a php Object format;

Json_decode ($ obj, true): converts a json object to an array;

$ Bad_json = "{'bar': 'baz '}";
$ Bad_json = '{bar: "baz "}';
$ Bad_json = '{"bar": "baz ",}';

 

If json_decode () is executed on these three strings, null is returned and an error is returned.

 

The first error is that the json separator (delimiter) only allows double quotation marks and does not support single quotation marks. The second error is the "name" of the json name-Value Pair (the part on the left of the colon). Double quotation marks must be used in any case. The third error is that you cannot add a comma (trailing comma) after the last value ).

 

In addition, json can only be used to represent objects and arrays. If json_decode () is used for a string or value, null is returned.

7. array intersection, difference set, and Union set

$ Inter_arr = array_intersect ($ m_arr, $ m_card): array intersection;
$ Diff_arr = array_diff ($ m_card, $ m_arr): array difference set;

The difference set of the array can only retrieve the values not in the first array. To obtain the difference set of all arrays, combine array_merge ();

$ Diff_arr = array_diff ($ m_card, $ m_arr );

$ Diff_arr2 = array_diff ($ m_arr, $ m_card );

$ Arr = array_merge ($ diff_arr, $ diff_arr2 );

 

$ Merge = array_merge (): array Union;

8. Replace the array value:

Array_replace ();

 

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.