简体   繁体   English

PHP如何过滤数组值

[英]PHP How to filter array values

I'm trying to show in a div 3 thumbnails stored in different folders. 我正在尝试在div 3中显示存储在不同文件夹中的缩略图。
In my bd determined (with value "1") which record has pictures. 在我的bd中确定(值为“ 1”)哪些记录具有图片。 So this is my db: 这是我的数据库:

+------+----------------+---------+--------+--------+
| ID   | BRAND          |   PHP   |  RUBY  |  JAVA  |
+------+----------------+---------+--------+--------+
| 1    | ford           |    1    |    0   |    0   |
+------+----------------+---------+--------+--------+
| 2    | seat           |    1    |    1   |    1   |
+------+----------------+---------+--------+--------+
| 3    | fiat           |    1    |    1   |    0   |
+------+----------------+---------+--------+--------+
| 4    | toyota         |    1    |    0   |    0   |
+------+----------------+---------+--------+--------+
| 5    | vw             |    1    |    0   |    1   |
+------+----------------+---------+--------+--------+

Selecting records: 选择记录:

$result = mysqli_query($connecDB,"SELECT * FROM brands WHERE php = '1' OR ruby = '1' OR java = '1' ORDER BY id ASC");


while($row = mysqli_fetch_assoc($result)){
    $new_array[] = $row;
}

But my problem is: How I can select the values ​​"1" and how to know which is which? 但是我的问题是:如何选择值``1''以及如何知道是哪个?
Maybe I could use: $row['php'] $row['java'] $row['ruby'] But how to filter the values ​​"0"? 也许我可以使用: $row['php'] $row['java'] $row['ruby']但是如何过滤值“ 0”呢?

It looks like you want to break up the array into php, java, and ruby. 看来您想将数组分成php,java和ruby。 Below is not tested, just to give you an idea. 下面没有经过测试,只是给您一个想法。

$resultsArray = array();
while($row = mysqli_fetch_assoc($result)){
    if($row['php'] == 1) {
        $resultsArray['php'][] = $row['id'];
    }
    if($row['java'] == 1) {
        $resultsArray['java'][] = $row['id'];
    }
    if($row['ruby'] == 1) {
        $resultsArray['ruby'][] = $row['id'];
    }
}

Then loop through that 2d array, grabbing the name from the key in resultsArray. 然后遍历该2d数组,从resultsArray的键中获取名称。

foreach($resultsArray as $language => $array) {
    foreach($array as $id) {
        echo "url/".$language."/".$id;
        echo "<br>";
    }
}
foreach($resultsArray as $language => $array){
    foreach(array_slice(glob('work/'.$language.'/'.$rowtest['id'].'/*.jpg'),0,1) as $image){    
        echo $image . "<br />";
    }
}

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

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