我有一个数组,我试图计算数组中对象键的数量,其中包含对象键' Value'不是0。
array (
0 =>
array (
'name' => 'item[110189]',
'value' => '0',
'primary_key' => '110189',
),
1 =>
array (
'name' => 'item[110190]',
'value' => '50',
'primary_key' => '110190',
),
2 =>
array (
'name' => 'item[110191]',
'value' => '0',
'primary_key' => '110191',
),
3 =>
array (
'name' => 'item[110192]',
'value' => '0',
'primary_key' => '110192',
),
)
我尝试过以下方法:
$input_items = array_filter($request->items, function($item){
$count = 0;
foreach($item as $i){
if(! $i['value'] == 0){
$count = $count + 1;
}
}
return $count;
}); // it will return an array
return $input_items;
我收到错误消息“无效对象'价值'说实话,我有一半的期待。
答案 0 :(得分:1)
您可以使用print array_reduce($request->items, function($carry ,$item){
if($item['value'] != 0){
$carry++;
}
return $carry;
},0);
:
aProducts(lRow) = Array("Id Product", "Value A", "Value B", "Value C")
'To copy value A
aProducts(lRow) = Array(.Cells(aPos(1)).Value2, .Cells(aPos(2)).Value2, Empty, Empty)
'To copy value B
aProducts(lRow) = Array(.Cells(aPos(1)).Value2, Empty, .Cells(aPos(3)).Value2, Empty)
'To copy value C
aProducts(lRow) = Array(.Cells(aPos(1)).Value2, Empty, Empty, .Cells(aPos(4)).Value2)
答案 1 :(得分:1)
以这种方式改变:
return JsonResponse(to_json, encoder=CustomEncoder)
完整代码
$input_items = array_filter($arr, function ($item) {
return ($item['value'] != 0);
});
输出只有非零条目:
<?php
$arr = array (
0 =>
array (
'name' => 'item[110189]',
'value' => '0',
'primary_key' => '110189',
),
1 =>
array (
'name' => 'item[110190]',
'value' => '50',
'primary_key' => '110190',
),
2 =>
array (
'name' => 'item[110191]',
'value' => '0',
'primary_key' => '110191',
),
3 =>
array (
'name' => 'item[110192]',
'value' => '0',
'primary_key' => '110192',
),
);
$input_items = array_filter($arr, function ($item) {
return ($item['value'] != 0);
});
print_r($input_items);