我有一个像这样的数组:-
[Column(DbType="INT NOT NULL IDENTITY", IsPrimaryKey=true, IsDbGenerated=true)]
public int ID {get; set;}
我该如何进行循环,以便最后我得到一定数量的a而只有正数。
答案 0 :(得分:2)
假设您的数组是这个,
$array = array(
array('amount' => 3, 'address' => 'a'),
array('amount' => 26, 'address' => 'a'),
array('amount' => 345, 'address' => 'a'),
array('amount' => -3, 'address' => 'a'),
array('amount' => 22, 'address' => 'a'),
);
您可以编写一个小的for
循环来实现这一目标,
$results = array();
foreach ($array as $k => $v){
if($v['amount'] > 0 && $v['address'] == 'a'){
$results[] = $v;
}
}
print_r($results);
这将为您提供amount
大于0且address
为a
的元素。这是您要找的吗?
答案 1 :(得分:2)
请问您想要的东西更精确吗?
假设您的数组位于名为$ _var的var中。
foreach($_var as $_array){
if($_array['amount'] > 0 && $_array['address']=='a'){ //if the amount is positiv and address = 'a'
$res[] = $_array; //Push the current item in your res array
}
}
$amount_of_a = count($res); //The number of a
print_r($res); //your result