Php 从字符串的分解数组中获取要比较的值

Php 从字符串的分解数组中获取要比较的值,php,Php,我从Mysql结果中得到以下字符串: Result1: 然后,我从另一个mysql结果中获得了一些ID,如下所示: 结果2: 然后我使用爆炸来分离结果1:。就像我得到了这个: $nota3 = explode("&", $nota); 1/test3& 2/test4& 现在,我正在使用foreach,然后使用另一个分解,以“/”分隔符分隔字符串 foreach ($nota3 as $key) { $nota4 = explode("

我从Mysql结果中得到以下字符串:

Result1:

然后,我从另一个mysql结果中获得了一些ID,如下所示:

结果2:

然后我使用爆炸来分离结果1:。就像我得到了这个:

$nota3 = explode("&", $nota);

1/test3&
2/test4&
现在,我正在使用foreach,然后使用另一个分解,以“/”分隔符分隔字符串

foreach ($nota3 as $key) {

$nota4 = explode("/", $key);

}
其结果如下(对于每次迭代的第一次):

好的,所以我无法将
nota4[0]
Result2:

我尝试过的事情:

  • 使用if并验证每个类型,将
    nota4[0]
    a
    $id
    转换为字符串

  • 尝试在数组中使用

  • 尝试使用strcmp($var1,$var2)

我错过了一些东西,但我真的不知道是什么

此外,当我尝试时,我无法将
nota4[0]
放入html字符串中,如

$nota5= nota4[0];

echo "<p id='mic' class='text-dark bg-warning'>".$nota5."</p>";
$nota5=nota4[0];
回声“

”$nota5.

”;

也许这有点傻,但我尝试了所有方法都没有成功。

你可以确保两者都是string和trim,并使用严格的运算符,即使它们似乎已经是string和trim了

$result1 = "1/test3&2/test4&";
$result2 = "1";

$id = trim((string) $result2);
foreach ($nota3 as $key) {
  $nota4 = explode("/", $key);
  if (trim((string) $nota4[0]) === $id) {
     //...
  }
}
这是另一种方法。这将设置$result1(或者您可以将其重命名)以成为键/值对的关联数组,允许您循环并比较像
$result2
这样的值与
$result1
数组中的键
id
。包括示例链接

<?php

$result1 = "1/test3&2/test4&";
$result2 = "1";

$result1 = array_map(function ($a) { 
    $tmp =  explode("/", $a);
    return array('id' => $tmp[0],'name' => $tmp[1]);
}, array_filter(explode("&", $result1)));

print_r($result1);
结果1的输出

[0] => Array
    (
        [id] => 1
        [name] => test3
    )

[1] => Array
    (
        [id] => 2
        [name] => test4
    )

我需要将结果1与result2@JuanFernandoz我再次更新了我的答案,专门回答了您的问题,并添加了另一种解析数据的方法。Sr,它非常有效,事实上我尝试了您的第一种解决方案,但使用了srtval而不是String()。我只是一名35岁的哥伦比亚律师,在这里为我的社区做一些有用的事情。非常感谢您的时间。我想这两种方法都可以——也许我第一次使用Srtval时遗漏了一些东西。也许是修剪或严格的opperator。
$result1 = "1/test3&2/test4&";
$result2 = "1";

$id = trim((string) $result2);
foreach ($nota3 as $key) {
  $nota4 = explode("/", $key);
  if (trim((string) $nota4[0]) === $id) {
     //...
  }
}
<?php

$result1 = "1/test3&2/test4&";
$result2 = "1";

$result1 = array_map(function ($a) { 
    $tmp =  explode("/", $a);
    return array('id' => $tmp[0],'name' => $tmp[1]);
}, array_filter(explode("&", $result1)));

print_r($result1);
foreach ($result1 as $item) {
  if ($item['id'] == $result2) {
    // $value 
  }
}
[0] => Array
    (
        [id] => 1
        [name] => test3
    )

[1] => Array
    (
        [id] => 2
        [name] => test4
    )