0

你好,所以我从我的数据库中展开了一行,我想根据展开的数组找到一个特定的值。

我的行示例。

    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”之类的内容。

我不知道是否可以从展开的数组中指定一个特定的名称。

4

4 回答 4

1

您可以使用 in_array 函数进行检查。由于您已经在数组 $a 中有数据

if(in_array("Easter Fonter", $a))
于 2013-09-18T04:46:27.147 回答
0
$arr = ["Josh Johnson", "Jenny Launcher", "Easter Fonter", "Eric Bennett"];

foreach($arr as $name) {
    if($name == "Easter Fonter") {
        echo $name + " was here";
    }
} 
于 2013-09-18T04:45:49.877 回答
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
}
于 2013-09-18T04:46:50.607 回答
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 "";
    }
?>
于 2013-09-18T04:47:21.577 回答