How can I shorten the code?

0 like 0 dislike
10 views
Hello, please tell me how can this code be shortened?
<?$invo = 0;foreach($invoices as $item): ?><? if (!($item['invoice_status'] == 1)) {continue;}?><? $invo=$invo+$item['invoice_ammount']; ?><?endforeach; echo $invo; ?>
by | 10 views

3 Answers

0 like 0 dislike
<?= array_sum(array_map(function ($item) {\rreturn $item['invoice_status'] == 1 ? $item['invoice_ammount'] : 0;\r}, $invoices)) ?>
by
0 like 0 dislike
<?=array_reduce($invoices, function(&$sum, $item) {\rreturn $sum + ($item['invoice_status'] == 1) ? $item['invoice_ammount'] : 0;\r})?>
by
0 like 0 dislike
You can use the: operator +=

Read more here:

<?$invo = 0;\rforeach($invoices as $item): ?>\r<? if (!($item['invoice_status'] == 1)) {\rcontinue;\r}?>\r<? $invo += $item['invoice_ammount']; ?>\r<?endforeach; echo $invo; ?>
by

Related questions

0 like 0 dislike
1 answer
asked Jun 9, 2019 by rusline18
0 like 0 dislike
1 answer
0 like 0 dislike
2 answers
0 like 0 dislike
4 answers
asked Jun 7, 2019 by hell0_w0rId
0 like 0 dislike
2 answers
asked May 21, 2019 by Sanchik97
110,608 questions
257,186 answers
0 comments
33,949 users