获取具有特定键/值对的二维数组中的子数组 [英] Get the sub array in a bidimensional array having a particular key/value pair

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

问题描述

我有一个大型的PHP数组,类似于:

I have a large PHP array, similar to:

$list = array(
    array(
        'id'     = '3243'
        'link'   = 'fruits'
        'lev'    = '1'
    ),
    array(
        'id'     = '6546'
        'link'   = 'apple'
        'lev'    = '2'
    ),
    array(
        'id'     = '9348'
        'link'   = 'orange'
        'lev'    = '2'
    )
)

我想获取包含特定id的子数组.

I want to get the sub-array which contains a particular id.

当前我使用以下代码:

$id = '3243'
foreach ($list as $link) {
    if (in_array($id, $link)) {
        $result = $link;
    }
}

它可以工作,但我希望有一种更好的方法.

It works but I hope there is a better way of doing this.

推荐答案

您可以

  • $link['id']==$id而不是in_array($id, $link)鞭子会更便宜.
  • $result = $link;之后添加break;指令,以避免无用的循环
  • write $link['id']==$id instead of in_array($id, $link) whitch will be less expensive.
  • add a break; instruction after $result = $link; to avoid useless loops

这篇关于获取具有特定键/值对的二维数组中的子数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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