Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use array in php

Shulou Source: shulou.com Published: 2022-06-03 04:10:13 10月05日 Update

This article will explain in detail how to use arrays in php. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

/ / 1 replace the key value size

$arr = array ('a'= > 1,'b' = > 2,'C'= > 3,'D' = > 4); $lower = array_change_key_case ($arr, CASE_LOWER); $upper = array_change_key_case ($arr, CASE_UPPER); / / var_dump ($lower); / / var_dump ($upper)

/ / 2 split the array into $size arrays. HasKay is whether to retain the original key value.

$arr2 = array ('name' = >' lixueyao', 'age' = > 24,' sex' = >'f'); $size = 2; $hasKey = true; $chunk = array_chunk ($arr2, $size, $hasKey); / / var_dump ($chunk)

/ / 3 the key specified in the multidimensional array can be generated into a new array, and the third parameter can be used as the key value of the new array.

$arr3s = array (array ('name' = >' lixueyao', 'age' = > 24,' sex' = >'f'), array ('name' = >' zhangsan', 'age' = > 25,' sex' = >'m'), array ('name' = >' lisi', 'age' = > 26,' sex' = >'f')); $column = array_column ($arr3s, 'age',' name') / / print_r ($column)

/ / 4 merge two arrays to form a new array, one as value and the other as key

$arrCombineOne = array ('one',' two', 'three'); $arrCombineTwo = array (' 1','2','3'); $combine = array_combine ($arrCombineTwo, $arrCombineOne); / / print_r ($combine)

/ / 5 returns the number of value occurrences in the array

$arrValue = array ('cased,' dumped, 'axed,' caged,'v'); $arrcnt = array_count_values ($arrValue); / / print_r ($arrcnt)

/ / 6 compare the difference of the array value, compare the parameters of one and two, what does not exist in the two array is the difference (compared with one and two, one is the compared array)

/ / if more than one array is compared, it is the difference of three arrays. (multiple arrays can be compared)

$arrDiffOne = array ('a'= > 'red', 'b' = >' blue', 'c' = > 'black', 'd' = >' purple'); $arrDiffTwo = array ('a'= > 'red',' b' = > 'blue', 'n' = >' black',); $arrDiffThree = array ('a'= > 'red', 'f' = >' hello','c'= > 'black'); $arrDiff = array_diff ($arrDiffOne, $arrDiffTwo, $arrDiffThree) / / print_r ($arrDiff)

/ / 7 compare the key and value of the array (both the same), and one is the array to be compared

/ / if there are three arrays to compare, you can first compare two arrays, and then use other arrays to compare the difference between the first two arrays

$arrAssoc = array_diff_assoc ($arrDiffOne, $arrDiffTwo, $arrDiffThree); / / print_r ($arrAssoc)

/ / 8 compare the key of the array, the principle is the same as above

$a4 = array ('d' = > 'name'); $arrKey = array_diff_key ($arrDiffOne, $arrDiffTwo, $arrDiffThree, $a4); / / print_r ($arrKey)

/ / 9 compare arrays, but compare key (custom functions are not very clear, one is compared

$uassocOne = array ('name' = >' zsy', 'age'= > 24,' say' = > 'hello'); $uassocTwo = array (' name' = > 'lixueyao',' age'= > 24); function myfunction ($a, $b) {$res = null; if ($a = = $b) {$res = 0;} $res = ($aqb)? 1RIMULI1 } $result=array_diff_ukey ($uassocOne,$uassocTwo, "myDiffKey"); print_r ($result); this is the end of the article on "how to use arrays in php". I hope the above content can be helpful to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Tags: Array number difference article parameter multiple more good practical same middle finger content function principle parameter comparison size that is article times knowledge Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn Linux Shulou Tech Info MariaDB OPPO Reno