<?php
function get_invoices() {
return [
[
"id" => 1,
"customer_id" => 81136,
],
[
"id" => 2,
"customer_id" => 81136,
],
[
"id" => 3,
"customer_id" => 81100,
],
];
}
function get_customer_invoices($customer_id = null) {
if ($customer_id) {
// return result of `array_filter` from `get_customer_invoices` function
return array_filter(get_invoices(), function ($item) use ($customer_id) {
// array_filter's callback should return `true` or `false`
return $item['customer_id'] === $customer_id;
});
} else {
return false;
}
}
var_dump(get_customer_invoices(81136));
- Output for 5.6.38, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.26, 8.4.1 - 8.4.13
- array(2) {
[0]=>
array(2) {
["id"]=>
int(1)
["customer_id"]=>
int(81136)
}
[1]=>
array(2) {
["id"]=>
int(2)
["customer_id"]=>
int(81136)
}
}
preferences:
55.13 ms | 408 KiB | 5 Q