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 ();