简体   繁体   中英

PHP: How to print row of multidimensional array with conditions

I'm a PHP beginner. I would like to print rows based on specific condition. For Example, I have a set of Data from SQL-Query(shown below) and I want to print all rows of [Series no] only when Type = 210 and Category = J. I know 'foreach' loop should be used in this case, but I do not know how.

来自 SQL 查询的表

And this is what I've tried. I know it is wrong but as I said I am a beginner and self learning.

$get_data = sqlsrv_query($connde, $mainquery);

$data_array = [];
while ($row = sqlsrv_fetch_array($get_data, SQLSRV_FETCH_ASSOC)) {
    $data_array[$row["Type"]][$row["Category"]][$row["Series No"]][$row["End Date"]];
}


foreach ($data_array as $value1) {
    if ($value1 = 210) {
        foreach ($value1 as $value2) {
            if isset($value2 == 'J')
            echo "<td>" . $row["Series No"] . "</td>";

        }
    }
}

You can deal with the rows directly:

<?php
$mainquery = 'SELECT `Type`, `Category`, `Series No`, `End Date` FROM `Foo`'; 
$get_data  = sqlsrv_query($connde, $mainquery);

while ($row = sqlsrv_fetch_array($get_data, SQLSRV_FETCH_ASSOC)) {
    if($row['Type'] == 210 && $row['Category'] == 'J') {
        echo "<td>" . $row['Series No'] . "</td>";
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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