你好,所以我从我的数据库中爆了一行,我想找到一个基于爆炸数组的特定值。
我的行示例。
Josh Johnson|Jenny Launcher|Easter Fonter|Eric Bennett
这是我的代码:
<?php
$rowexplode = $row['name'];
$a = explode("|",$rowexplode);
if(count($a)>1) {
$explode_results = $rowexplode;
$explode_array = str_replace("|",", ", $explode_results);
echo $explode_array;
}
else {
echo "";
}
?>
这就是它显示的内容
Josh Johnson, Jenny Launcher, Easter Fonter, Eric Bennett
现在我希望它能抓住其中一个名字并显示出来。 例如。从列表中抓住Easter Fonter并回复“Easter Fonter was here”这样的内容。
我不知道是否可以从爆炸阵列中指定特定名称。
答案 0 :(得分:1)
您可以使用in_array函数进行检查。由于您已经拥有数组$ a
中的数据if(in_array("Easter Fonter", $a))
答案 1 :(得分:0)
$arr = ["Josh Johnson", "Jenny Launcher", "Easter Fonter", "Eric Bennett"];
foreach($arr as $name) {
if($name == "Easter Fonter") {
echo $name + " was here";
}
}
答案 2 :(得分:0)
有助于了解您在每一步中创建的内容:
<?php
$rowexplode = $row['name']; // $rowexplode is now a string
$a = explode("|",$rowexplode);
// $a is an array with strings, such as:
// array('Josh Johnson, 'Jenny Launcher', 'Easter Fonter', 'Eric Bennett')
if(count($a)>1) {
$explode_results = $rowexplode;
// $explode_results is now just a copy of $rowexplode, still just a string
$explode_array = str_replace("|",", ", $explode_results);
// This says array, but it isn't. It's just a string with the pipes replaced:
// "Josh Johnson, Jenny Launcher, Easter Fonter, Eric Bennett"
echo $explode_array;
// Output that string
}
所以,如果你想要这些值,你可以这样做:
foreach ($a as $name) {
echo "$name was here\n"; // Echo each name one at a time
}
答案 3 :(得分:0)
这可能有帮助
//your text here
$rowexplode = 'Josh Johnson|Jenny Launcher|Easter Fonter|Eric Bennett';
$a = explode("|",$rowexplode);
if(count($a)>1) {
//your search string
$name = "Easter Fonter";
//check here
if(in_array($name,$a))
{
echo $name." was here.";
}else{
echo "Name Not Found".implode(', ', $a);
}
}
else {
echo "";
}
?>