【问题标题】:Echo selective data (rows) from multidimensional array in PHP从 PHP 中的多维数组中回显选择性数据(行)
【发布时间】:2012-04-24 15:19:41
【问题描述】:

我有一个来自 .csv 文件的数组。这些来自一个房地产项目。在第二列中,我有“待售”字样,在第三列中,“出租”字样仅在相关行上显示。否则单元格为空。例如,我只想显示列表行 For Sale。当然,如果用户单击其中一行中的链接,则会显示相应的页面。 我似乎无法定位列中的文本,也不允许在整个数组中使用 For Sale 字样,因为它们可能出现在另一列中(例如描述)。 我试过这个,但没有用。

/* The array is $arrCSV */
foreach($arrCSV as $book) {
    if($book[1] === For Sale) {
      echo '<div>';
    }
    echo '<div>';
    echo $book[0]. '<br>';
    echo $book[1]. '<br>';
    echo $book[2]. '<br>';
    echo $book[3]. '<br>';
    echo $book[6]. '<br><br><br>';
    echo '</div>';
}

我也试过这个:

foreach($arrCSV as $key => $book) {
    if($book['1'] == 'For Sale') {
      echo '<div>';
    }
    echo '<div>';
    echo $book[0]. '<br>';
    echo $book[1]. '<br>';
    echo $book[2]. '<br>';
    echo $book[3]. '<br>';
    echo $book[6]. '<br><br><br>';
    echo '</div>';
}

【问题讨论】:

    标签: php arrays csv selection


    【解决方案1】:

    你的意思是:

    foreach($arrCSV as $key => $book) {
        if($book['1'] == 'For Sale') {
            echo '<div></div>';
        }else{
            echo '<div>';
            echo $book[0]. '<br>';
            echo $book[1]. '<br>';
            echo $book[2]. '<br>';
            echo $book[3]. '<br>';
            echo $book[6]. '<br><br><br>';
            echo '</div>';
        }
    }
    

    【讨论】:

    • 我真的不需要 else 条件。我只想有选择地显示某些行(或行)。发布后我找到了解决方案
    • 您应该发布您的解决方案,以便anybody searching 能够看到它
    • @ianbarker:我试过了,但是因为我的排名不到100,所以我必须等待8个小时。
    【解决方案2】:

    我在这里找到了解决方案: PHP: Taking Array (CSV) And Intelligently Returning Information

    $searchCity = 'For Sale'; //or whatever you are looking for
    $file = file_get_contents('annonces.csv');
    $results = array();
    $lines = explode("\n",$file); 
    //use any line delim as the 1st param,
    //im deciding on \n but idk how your file is encoded
    
    foreach($lines as $line){
    //split the line
    $col = explode(";",$line);
    //and you know city is the 3rd element
    if(trim($col[1]) == $searchCity){
         $results[] = $col;
    }
    }
    

    然后:

    foreach($results as $Rockband)
    {
    echo "<tr>";
    foreach($Rockband as $item)
    {
    echo "<td>$item</td>";
    }
    echo "</tr>";
    }
    

    如您所见,我正在学习。事实证明,通过制定一个问题,会显示一系列类似的帖子,这比使用google要快得多,也容易得多。谢谢。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-11-24
      • 1970-01-01
      • 1970-01-01
      • 2015-12-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多