3

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.

2 Answers 2

1

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/>";
        }
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.