具有特定条件的PHP数组打印值

时间:2015-07-10 03:26:39

标签: php arrays if-statement foreach

我有一个数组$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 ))

我想打印TRX的{​​{1}}。我在foreach中使用条件语句如下:

cust_type = Corporate

但是它会打印所有TRX值而不是公司值。 请帮助我,谢谢。

2 个答案:

答案 0 :(得分: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 :(得分:1)

==

中使用双=代替单if($value['cust_type'] == 'Corporate')
相关问题