2015-07-10 33 views
3

PHP數組打印值我有一個數組$t1象下面這樣:與特定條件

Array ( 
    [0] => Array ( 
     [cust_type] => Corporate 
     [trx] => 1 
     [amount] => 10) 
    [1] => Array ( 
     [cust_type] => Non Corporate 
     [trx] => 2 
     [amount] => 20) 
    [2] => Array ( 
     [cust_type] => Corporate 
     [trx] => 3 
     [amount] => 30) 
    [3] => Array ( 
     [cust_type] => Non Corporate 
     [trx] => 4 
     [amount] => 40)) 

我想打印TRXcust_type = Corporate。我使用foreach內的條件語句如下:

foreach ($t1 as $key => $value) { 
     if($value['cust_type'] = 'Corporate') 
      { 
       print_r($value['trx']); 
      } 
     echo "<br/>"; 
    } 

但它打印所有TRX值而不是公司只。 請幫助我,謝謝。

回答

1

使用

if($value['cust_type'] == 'Corporate') 

,而不是

if($value['cust_type'] = 'Corporate') 

所以最終的結果將是

foreach ($t1 as $key => $value) { 
      if($value['cust_type'] == 'Corporate') 
       { 
        print_r($value['trx']); 
       } 
      echo "<br/>"; 
     } 
1

使用雙==代替單一=if($value['cust_type'] == 'Corporate')