Given an array: [ {id: 1, status: 0, orderItemId: 1, date: "2015-01-02"}, ]
- User
Given an array: [ {id: 1, status: 0, orderItemId: 1, date: "2015-01-02"}, ]
- Assistant
You provided an array with one object:
[ {id: 1, status: 0, orderItemId: 1, date: "2015-01-02"}, ]How can I assist you with this array? Are you looking to perform some operations or transformations on it?
- User
Given an array: [ {id: 1, status: 0, orderItemId: 1, date: "2015-01-02"}, {id: 2, status: 1, orderItemId: 2, date: "2015-01-02"}, {id: 3, status: 3, orderItemId: 2, date: "2015-01-02"}, {id: 4, status: 2, orderItemId: 1, date: "2015-01-02"}, ]
How can I check if specific status found in array for the specific orderitemid in PHP? for example, if check for the status 1 in orderitemid 2, should return true; for the status 3 in orderitemid 1 should return false
- Assistant
To check if a given status exists for a specific
orderItemIdin your array, you can loop through the array and check both conditions. Here's a sample PHP function that does this:<?php $array = [ ['id' => 1, 'status' => 0, 'orderItemId' => 1, 'date' => "2015-01-02"], ['id' => 2, 'status' => 1, 'orderItemId' => 2, 'date' => "2015-01-02"], ['id' => 3, 'status' => 3, 'orderItemId' => 2, 'date' => "2015-01-02"], ['id' => 4, 'status' => 2, 'orderItemId' => 1, 'date' => "2015-01-02"], ]; function checkStatusForOrderItemId($array, $status, $orderItemId) { foreach ($array as $item) { if ($item['orderItemId'] == $orderItemId && $item['status'] == $status) { return true; } } return false; } // Usage examples var_dump(checkStatusForOrderItemId($array, 1, 2)); // true var_dump(checkStatusForOrderItemId($array, 3, 1)); // falseExplanation:
- The function accepts the array, the status to check, and the
orderItemIdto check against. - It loops through each item, and if it finds an item where both
orderItemIdandstatusmatch, it returns .
- The function accepts the array, the status to check, and the