如何通过二维数组中的id获取标题? [英] How to Get title by id in 2D array?

查看:55
本文介绍了如何通过二维数组中的id获取标题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数组,

array
(
    [0] => array
    (
        [id]       => 2
        [title]    => Test
        [alt_text] => 'This is test',

    )

    [1] => array
    (
        [id]       => 3
        [title]    => Test1
        [alt_text] => 'This is test1',

    )

    [2] => array
    (
        [id]       => 7
        [title]    => Test2
        [alt_text] => "This is test2",

    ),

)

我想要动态 ID 的标题值.

I want the value of the title for dynamic id.

例如如果我通过 2,那么它应该返回标题的值作为测试

E.g. If I pass 2, then it should return value of title as Test

我可以用 foreach 循环做到这一点,没有问题.但是是否有任何核心功能可以实现这一点,或者核心功能的组合或单行代码片段?

I can do this with foreach loop no issue. But is there any core function to achieve this or a combination of core functions or a one-liner snippet?

推荐答案

这是我最终使用的解决方案:

This is the solution I ended up using:

echo $arr[array_search(7, array_column($arr, 'id'))]['title'];

这篇关于如何通过二维数组中的id获取标题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆