为了账号安全,请及时绑定邮箱和手机立即绑定

php txt 字符串比较数组比较

php txt 字符串比较数组比较

PHP
炎炎设计 2023-05-26 17:43:39
$c = '4432';$word = file_get_contents('1234.txt');$value = explode(" ",$word);for ($i = 0; $i < 3; $i++) {   if ($value[$i] = $c){      echo $value[$i];      break;    }}文件1234.txt内容:1234 789 4432 9985532 999如何比较4432并分别得到值789?我需要:if $c is 1234 then get 789 valueif $c is 4432 then get 998 valueif $c is 5532 then get 999 value现在我的代码只能得到 1234 或 4432 或 5532。
查看完整描述

1 回答

?
BIG阳

TA贡献1859条经验 获得超6个赞

file()您可以尝试使用和功能获得预期的结果explode():


<?php

$match = '4432';


$file = file('1234.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);

foreach ($file as $line) {

    $words = explode(" ", $line);

    if ($words[0] == $match) {

        echo $words[1];

        break;

    }       

}

?>

补充笔记:


函数file_get_contents()将整个文件读入一个字符串,但file()将文件的内容返回到一个数组中。

PHP 中的比较运算符==是( ===),而不是=。


查看完整回答
反对 回复 2023-05-26
  • 1 回答
  • 0 关注
  • 73 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信