带有特定条件的PHP数组打印值
我有一个数组带有特定条件的PHP数组打印值,php,arrays,if-statement,foreach,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
$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
谁的cust\u type=Corporate
。我在foreach中使用条件语句,如下所示:
foreach ($t1 as $key => $value) {
if($value['cust_type'] = 'Corporate')
{
print_r($value['trx']);
}
echo "<br/>";
}
foreach($t1作为$key=>$value){
如果($value['cust_type']='Corporate')
{
打印($value['trx']);
}
回声“
”;
}
但它打印所有TRX值,而不是仅打印公司值。
请帮助我,谢谢。使用
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/>";
}
foreach($t1作为$key=>$value){
如果($value['cust_type']=='Corporate')
{
打印($value['trx']);
}
回声“
”;
}
如果($value['cust\u type']='Corporate'),则在中使用双=
代替单=