PHP CSV VLookup [英] PHP CSV VLookup

查看:183
本文介绍了PHP CSV VLookup的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在查找一个PHP函数,它可以读取CSV文件,并对第1列执行vlookup,以回显第2列中同一行的相关值。

I'm looking a PHP function that can read a CSV file and perform a vlookup on column 1 to echo the relating value on the same row in column 2.

例如,如果CSV包含:

For example, if the CSV contains:

Name,Email
John,john@domain1.com
Frank,frank@domain2.com
Julie,julie@domain3.com

类似:

<?php
$name = "Name to be inserted";
$csv = 'filename.csv';

*function to vlookup $name in $csv, get column 2 value and pipe to $email* 

echo $email;
?>

任何人都可以建议一个可以完成上述功能的功能?

Can anyone suggest a function that can accomplish the above?

谢谢

推荐答案

$csv = array_map('str_getcsv', file('test.csv'));
$findName="Frank";
foreach($csv as $values)
{
  if($values[0]==$findName)   // index 0 contains the name
    echo $values[1];          // index 1 contains the email  
}

请注意,是特定于你给的csv格式。如果格式更改,您将必须调整索引。

Please note that the indexes used in this answer are specific to the csv format you gave. You will have to adjust the indexes if format changes.

这篇关于PHP CSV VLookup的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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