与另一个数组相比,如何在数组中寻找价值 [英] How to find value in array compared to another one

查看:51
本文介绍了与另一个数组相比,如何在数组中寻找价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

与其他方法相比,我正在寻找一种在数组中查找值的方法.

I'm looking for a way to find value in array compared to another one.

我们假设我们有一个php数组:(我选择的是 this a>问题,因为这正是我的情况

Let's assume we have a php array : (I take the one of this question because it's exactly my case)

$userdb = array(
    array(
        'uid' => '100',
        'name' => 'Sandra Shush',
        'pic_square' => 'urlof100'
    ),
    array(
        'uid' => '5465',
        'name' => 'Stefanie Mcmohn',
        'pic_square' => 'urlof100'
    ),
    array(
        'uid' => '40489',
        'name' => 'Michael',
        'pic_square' => 'urlof40489'
    )
);

我需要知道是否有一种方法可以找到(例如)uid为100的name值.

What's I need is to know if there's a way to find (for example) the value of name where uid is 100.

我希望情况会很清楚,谢谢您的帮助:)

I hope to have been clear, thanks for your help :)

推荐答案

是的,有办法

$temp = array_column($userdb, 'name','uid');
echo ($temp[100] ?? ''); // php 7  
echo (!empty($temp[100]) ? $temp[100] : ''); // < php 7

array_column —从输入数组的单个列中返回值

array_column — Return the values from a single column in the input array

语法:

array_column ( array $input , mixed $column_key [, mixed $index_key = NULL ] ) : array

演示.

这篇关于与另一个数组相比,如何在数组中寻找价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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