简体   繁体   English

从PHP中的多维数组回显选择性数据(行)

[英]Echo selective data (rows) from multidimensional array in PHP

I have an array coming from a .csv file. 我有一个来自.csv文件的数组。 These are coming from a real estate program. 这些来自房地产计划。 In the second column I have the words For Sale and in the third column the words For Rent that are indicated only on the rows that are concerned. 在第二列中,我有“待售”一词,在第三列中,“有待出租”一词仅在相关行上显示。 Otherwise the cell is empty. 否则,单元格为空。 I want to display a list rows only For Sale for example. 例如,我只想显示列表行。 Of course then if the user clicks on a link in one of the rows, the appropriate page will be displayed. 当然,如果用户单击其中某一行中的链接,将显示相应的页面。 I can't seem to target the text in the column, and I can't permit that the words For Sale be used throughout the entire array because they could appear in another column (description for example). 我似乎无法将列中的文本作为目标,也不允许在整个数组中使用“待售”一词,因为它们可能会出现在另一列中(例如说明)。 I have tried this, but to no avail. 我已经尝试过了,但是无济于事。

/* 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>';
}

I also tried this: 我也试过这个:

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>';
}

Do you mean: 你的意思是:

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>';
    }
}

I found a solution here: PHP: Taking Array (CSV) And Intelligently Returning Information 我在这里找到了一个解决方案: PHP:采用数组(CSV)并智能地返回信息

$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;
}
}

And then: 接着:

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

As you can see I'm learning. 如您所见,我正在学习。 It turns out that by formulating a question, a series of similar posts are displayed, which is much quicker and easier than using google. 事实证明,通过提出问题,将显示一系列类似的帖子,这比使用Google更快,更容易。 Thanks. 谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM