doumen6532 2017-09-29 20:23
浏览 30

当有时值不存在时,在PHP中组合数组值

I apologize if this question has a no brainer answer. I am still learning more ins and outs of php.

I have a snippet of code that is taking in a CSV file. This CSV file is uploaded by a user who downloads it from an external source. In the CSV file, the person's first name and last name is not split in separate columns. Therefore, in PHP the following is used:

$member_name = explode( " ", $application_data[5]);

The problem is that when this data is then used to render a PDF document to send a letter to the member, it cuts off their last name if their last name is two words.

The information is loaded into the PDF document with the first name and last name field by using:

$member_name[0],
$member_name[1]                         

Can I safely do:

$member_name[0],    
$member_name[1] + $member_name[2]

Even if 99% of the members do not have a space in the last name? Will I get an error that member_name[2] doesn't exist 99% of the time this is done?

I've done some searching on array_merge. Is that a better option? I've been trying to search for how php handles when you add something that doesn't exist and I'm drawing a blank.

I don't want to assume my solution will work and then when the person uploads their CSV file tomorrow, they get an error.

Or maybe I'm looking at this the wrong way and before it attempts to render a pdf document, I should do an if statement that figures out if $member_name[2] exists.

Thank you!

  • 写回答

2条回答 默认 最新

  • douao2019 2017-09-29 20:28
    关注

    You could just use a limiter on your explode to only seperate on the first space. Here is an example.

    $name = "George The King";
    print_r(explode(' ', $name, 2)); //prints -> Array ( [0] => George [1] => The King )
    
    评论

报告相同问题?

悬赏问题

  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错