Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/245.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何检查所有PHP explode的字符串_Php - Fatal编程技术网

如何检查所有PHP explode的字符串

如何检查所有PHP explode的字符串,php,Php,我用php分解了一个文本文件。我的目标是对照一个值检查每个字符串,如果“分解字符串”中存在该值,则重新分配该值: 我正在检查的值:“am、B、L、PM” 目标:将“AM、B、L、PM”的所有分解字符串转换为该“AM、B、L” 伪逻辑: if ($pieces[2] = "AM,B,L,PM") { $pieces[2] = "AM,B,L" } 上述代码适用于单个$pieces[2],但我想检查所有的$pieces、$pieces[3]、$pieces[4]、$pieces[5] 请建议试

我用php分解了一个文本文件。我的目标是对照一个值检查每个字符串,如果“分解字符串”中存在该值,则重新分配该值:

我正在检查的值:“am、B、L、PM” 目标:将“AM、B、L、PM”的所有分解字符串转换为该“AM、B、L”

伪逻辑:

if ($pieces[2] = "AM,B,L,PM")
{
  $pieces[2] = "AM,B,L"
}
上述代码适用于单个$pieces[2],但我想检查所有的$pieces、$pieces[3]、$pieces[4]、$pieces[5]

请建议试试这个

$l = count($pieces);

for($x=0;$l<$x;$x++) {

    if ($pieces[$x] == "AM,B,L,PM") {
        $pieces[$x] = "AM,B,L";
    }

}
$l=计数(件);

对于($x=0;$l,您可以使用
foreach
循环数组,如果您的条件为
true
,则为原始数组指定一个值,最后再次在
字符串中转换数组

foreach ($pieces as $key => $piece) {
    if (trim($piece) == "AM,B,L,PM") {
      $pieces[$key] = "AM,B,L";
    }
}

// In the first parameter you put the delimiter you used to explode the string 
$string = implode(' ', $pieces);
$string=str_replace('AM,B,L,PM','AM,B,L',$pieces)

echo$string[370]


解决了。谢谢大家。

FOR loop有什么问题吗?@RiggsFolly愚蠢的问题,但你能告诉我怎么做吗。有一个非常非常秘密的地方,你可以查这些东西ssshhhh不要告诉任何人关于它的事我注意到你两年来从未接受过答案,因此你得到的是评论而不是答案。请阅读太多的问题,n一个可接受的答案包含一个解释,而不仅仅是“试试这个”(这无助于OP理解发生了什么)。如果
中有两个
==
(比较)当您设置值时,还有一个
=
。现在您实际上没有设置任何东西。一点合理的代码缩进将使您的代码更可读、更易懂和更易于维护。我正在尝试您的代码;但是,当我这样做时:echo$pieces[370]//输出:“am,B,L,PM”Sry Bro,我不知道为什么,但也许有机会在你的字符串中使用一些特殊字符,所以试试这个$a=preg_替换(“/[^a-zA-Z]/”,“,$pieces[$x]);if($a==“amplpm”){$pieces[$x]=“AM,B,L”}
foreach ($pieces as $key => $piece) {
    if (trim($piece) == "AM,B,L,PM") {
      $pieces[$key] = "AM,B,L";
    }
}

// In the first parameter you put the delimiter you used to explode the string 
$string = implode(' ', $pieces);