This is how I am retrieving items from an array in JavaScript, it works fine:
function getLibItemByName(name){
var index = json.library.findIndex(p => p.name == name);
return json.library[index];
}
my array is like:
[
{
"name": "item1"
},
{
"name": "item2"
},
...
]
Now I am wondering how I can do the same in PHP?
Update: I came up with this that is working for now
function getLibItemByName($name){
global $json;
foreach($json['library'] as $key => $val){
if($name === $val['name']){
return $json['library'][$key];
}
}
return false;
}
array_search()? Please set up a php sample. There may be more than one way to do it and it depending on your data structure and values, one way may be better than another.array_filterand then grabbing the first key from the result will do what you want, but that might not be performant depending on the size of your array.array_searchcombined witharray_columnthen.