简体   繁体   中英

PHP foreach filtered by condition

I have multidimensional array with results and I would like to filter these based on given category. Just like we would do with “WHERE” in MySQL.

<?php 
$links_cat = 1;
foreach($linkcreator as $x=>$link)
  {
  echo "Category: " . $link[0] . " - <a href='" . $link[1] . "'>" . $link[2] , "</a>";
  echo "<br>";
  }
?>

$links_cat is corresponding with $link[0] . How do I achieve that?

With an if statement

<?php 
$links_cat = 1;
foreach($linkcreator as $x=>$link)
{
    if($link[0] == $links_cat){
        echo "Category: " . $link[0] . " - <a href='" . $link[1] . "'>" . $link[2] , "</a>";
        echo "<br>";
    }
}
?>

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