2016-11-08 239 views
-6

thrue milti陣列我需要環通用foreach和只列出汽車陣列是年輕的,然後2000PHP循環用foreach

$cars = array(array("model"=>"Volvo","year"=>2010),array("model"=>"BMW","year"=>1995),array("model"=>"Saab","year"=>1998),array("model"=>"Land Rover","year"=>2004) 
+2

請參考[的foreach] PHP文檔(http://php.net/manual/en/control-structures.foreach.php)和[比較運算符(http://php.net/手冊/ en/language.operators.comparison.php),祝您有愉快的一天。 – cteski

回答

1
<?php 
$cars = array(
    array(
     "model" => "Volvo", 
     "year" => 2010 
    ) , 
    array(
     "model" => "BMW", 
     "year" => 1995 
    ) , 
    array(
     "model" => "Saab", 
     "year" => 1998 
    ) , 
    array(
     "model" => "Land Rover", 
     "year" => 2004 
    ) 
); 


foreach($cars as $car){ 
    if($car['year'] > 2000){ 

//Write code to do what you want to do with the car. Next line just prints it. 

     echo $car['model'].'</br>' ; 
    } 
} 

輸出:

Volvo 
Land Rover 

的foreach循環需要每在另一個陣列的$ car變量中的汽車。在foreach中檢查模型中的值。如果年齡小於2000年,那麼做任何你想做的事情。

0
<?php 

$cars = array(array("model"=>"Volvo","year"=>2010),array("model"=>"BMW","year"=>1995),array("model"=>"Saab","year"=>1998),array("model"=>"Land Rover","year"=>2004)); 

$size = sizeof($cars); 

echo "You have $size cars in the array\n"; 

$i=0; 
foreach($cars as $car) 
{ 
    if($car['year'] < 2000) 
    { 
      echo $car['model'] .' - '.$car['year']."\n"; 
    } 
}