How to find the value corresponding to key in array
Editor to share with you how to find the value corresponding to key in array. I believe most people don't know much about it, so share this article for your reference. I hope you can learn a lot after reading this article. Let's learn about it together.
The array_search () function searches an array for a specified value of key, and returns the corresponding key if found, otherwise it returns FALSE1. Search for associative array keys if you find a specified key in an array
Find the value corresponding to key in array
Gets the value corresponding to the key in the array
Copy the code
ClassTest {
Publicfunctionindex () {
$info=array (
'name'= >' daicr'
'age'= > 24
)
$res=$this- > array_get_value ($info,'name','chrdai')
Echo$res
}
/ * *
* @ purpose: gets the value corresponding to the key in the array
* @ param:array$array array
Keys in * @ param:string$name array
* @ param:string$defaultValue default
* @ return: the value corresponding to the key in the array
* /
Functionarray_get_value ($array,$name,$defaultValue=null) {
Returnis_array ($array)? (isset ($array [$name])? $array [$name]: $defaultValue): $defaultValue
}
}
$test=newTest ()
$test- > index ()
Find all matching objects in the array object based on the key value (recursion)
FunctionparseJson (jsonObj,key,value) {
/ / Loop all keys
Letarray= []
For (letvinjsonObj) {
Letelement=jsonObj [v]
/ / 1. Judge whether it is an object or an array
If (typeof (element) = = 'object') {
Letresult=parseJson (element,key,value)
If (result.length) {
Array=array.concat (result)
}
The above is all the contents of the article "how to find the value corresponding to key in array". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!