I'm currently working with Twig. in an array of json can show 3 different types of elements.
"items":[
{
"type": "youtube"
},
{
"type": "Picture"
},
{
"type": "Gallery"
}
]
I want to first select the type youtube in case there is no youtube select picture.
答案 0 :(得分:0)
如果您使用PHP 7+来修饰数组,然后在其中搜索值“ youtube”,则可以使用array_column。
如果找不到,则搜索“图片”。
$arr = json_decode($str, true);
$type = array_column($arr, 'type');
$key = array_search('youtube', $type);
if($key === false){
$key = array_search('Picture', $type);
}
返回值是您要查找的$ arr中的键。
像var_dump($arr[$key]);