doupin5408 2017-03-10 03:22
浏览 120
已采纳

使用in_array()的2个值的PHP搜索数组列

How do I search for 2 values in the same record using in_array()? For example I want to search for 'AuthorNiceName' value and 'AuthorType' value.

The below code does not work how I would like. It should return "EU TEST" but it returns "CP TEST".

Thanks in advance.

 $authors = array(
        array(
            'AuthorName' => 'John Smith',
            'AuthorNiceName' => 'john-smith',
            'AuthorType' => 'CP'
        ),
        array(
            'AuthorName' => 'Joe Bloggs',
            'AuthorNiceName' => 'joe-bloggs',
            'AuthorType' => 'EU'
        ),
    );



    if (in_array('joe-bloggs', array_column($authors, 'AuthorNiceName')) && in_array('EU', array_column($authors, 'AuthorType'))) {
        $authorType = 'CP TEST';
    }
    else {
        $authorType = 'EU TEST';
    }

    echo $authorType;

UPDATE: My latest code using @Philipp's suggestion which I adjusted slightly. However it doesn't work, if there are other users with the same 'AuthorType' it returns "no match"?

$authors = array( //TODO
    array(
        'AuthorName' => 'John Smith',
        'AuthorNiceName' => 'john-smith',
        'AuthorType' => 'CP'
    ),
    array(
        'AuthorName' => 'Joe Bloggs',
        'AuthorNiceName' => 'joe-bloggs',
        'AuthorType' => 'EU'
    ),
    array(
        'AuthorName' => 'Matt Bat',
        'AuthorNiceName' => 'matt-bat',
        'AuthorType' => 'EU'
    ),    
);

$name = 'joe-bloggs';
$type = 'EU';
foreach ($authors as $author) {
    if ($author['AuthorNiceName'] == $name && $author['AuthorType'] == 'EU') {
        $authorType = 'EU Test';
    }
   elseif ($author['AuthorNiceName'] == $name && $author['AuthorType'] == 'CP') {
        $authorType = 'CP Test';
    }
    else {
        $authorType = 'no match';
    }

}

echo $authorType; //returns "not match". it should return "EU Test".

展开全部

  • 写回答

1条回答 默认 最新

  • douyuan4357 2017-03-10 03:32
    关注

    It seems, you should use a loop for what you want

    $name = 'joe-bloggs';
    $type = 'EU';
    foreach ($authors as $author) {
        if ($author['AuthorNiceName'] == $name && $author['AuthorType'] == $type) {
            // do something
        }
    }
    

    using in_array with array_column looses the information if the result is from the same column. Using array_search, you could also compare the index of the columns, but this seems like a bad solution...


    Update

    The problem with your update is, you overwrite the $authorType var, after you set the correct value. You should check, if an value is already set and only then set a new value.

    $name = 'joe-bloggs';
    $authorType = false;
    foreach ($authors as $author) {
        if ($author['AuthorNiceName'] == $name && $author['AuthorType'] == 'EU') {
            $authorType = 'EU Test';
        } elseif ($author['AuthorNiceName'] == $name && $author['AuthorType'] == 'CP') {
            $authorType = 'CP Test';
        }
    }
    
    if ($authorType === false) {
        echo 'no match';
    } else {
        echo $authorType;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

悬赏问题

  • ¥15 Linux误删文件,请求帮助
  • ¥15 IBMP550小型机使用串口登录操作系统
  • ¥15 关于#python#的问题:现已知七自由度机器人的DH参数,利用DH参数求解机器人的逆运动学解目前使用的PSO算法
  • ¥15 发那科机器人与设备通讯配置
  • ¥15 Linux环境下openssl报错
  • ¥15 我在使用VS编译并执行之后,但是exe程序会报“无法定位程序输入点_kmpc_end_masked于动态链接库exe上“,请问这个问题有什么解决办法吗
  • ¥15 el-select光标位置问题
  • ¥15 单片机 TC277 PWM
  • ¥15 在更新角色衣服索引后,Sprite 并未正确显示更新的效果该如何去解决orz(标签-c#)
  • ¥15 VAE代码如何画混淆矩阵
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部