Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP array print value with specific condition

i have an array $t1 like 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 use conditional statement inside foreach as 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 corporate only. Kindly help me, thank you.

like image 326
King Goeks Avatar asked Jul 30 '26 16:07

King Goeks


2 Answers

use

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

instead of

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

So the final result will be

   foreach ($t1 as $key => $value) {
            if($value['cust_type'] == 'Corporate')
                {
                    print_r($value['trx']);
                }
            echo "<br/>";
        }
like image 148
Kanishka Panamaldeniya Avatar answered Aug 01 '26 06:08

Kanishka Panamaldeniya


Use double == in place of single = in if($value['cust_type'] == 'Corporate')

like image 44
Vikas Umrao Avatar answered Aug 01 '26 04:08

Vikas Umrao