【问题标题】:PHP array print value with specific condition具有特定条件的PHP数组打印值
【发布时间】:2015-09-28 16:22:12
【问题描述】:

我有一个数组$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 值而不是仅打印公司值。 请帮助我,谢谢。

【问题讨论】:

    标签: php arrays if-statement foreach


    【解决方案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/>";
            }
    

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 2019-09-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-07-06
        相关资源
        最近更新 更多