PHP array array value with specific condition

i has an array $t1

as shown below:

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 ))

      

I want to print TRX

whose cust_type = Corporate

. I am using conditional expression inside foreach as shown below:

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

      

But it prints all TRX values ​​instead of the corporate one. Please help me, thanks.

+3


source to share


2 answers


use

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

      

instead



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

      

So the end result will be

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

      

+1


source


Use double ==

instead of single =

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



+1


source







All Articles