I am basing my code on the following link: PHP Multidimensional Array Searching (Find key by specific value) I attempted to add code similar to Get most recent date from an array of dates ; however I kept receiving illegal offset errors.
I can successfully select the proper value based on the first code. How can I then narrow down my return based on the most recent date in the array?
I am using php 5.4.45 and my array is always sorted by date with the most recent being last. I cannot simply select the last array index as it is not always a "Paid" order status.
My Array:
array(4) {
[0]=> array(3) {
["status"]=> string(6) "Active"
["check_number"]=> string(0) ""
["date_added"]=> string(19) "17/11/2015 10:34:53" }
[1]=> array(3) {
["status"]=> string(4) "Paid"
["check_number"]=> string(5) "12345"
["date_added"]=> string(19) "14/12/2015 07:40:59" }
[2]=> array(3) {
["status"]=> string(8) "Invoiced"
["check_number"]=> string(0) ""
["date_added"]=> string(19) "14/12/2015 09:44:31" }
[3]=> array(3) {
["status"]=> string(4) "Paid"
["check_number"]=> string(4) "0258"
["date_added"]=> string(19) "14/12/2015 09:53:29" }
}
My code:
function getValue($fromSource, $field, $value, $get){
if ($fromSource[$field] === $value) {
return $fromSource[$get];
};
return false;
}
foreach ($order['order_history'] as $order_history) {
var_dump(getValue($order_history, "status", "Paid", "check_number"));
}
Desired result: "0258" (where status = "Paid" and date = most recent)
Currently Receiving: "12345" "0258" (where status = "Paid")