Comparing arrays, point me in the right direction please
So what im trying to do is compare bank transactions to orders in my database and if they match change the order status to confirmed.
The transactions come in a xml format
$transaction->user-number //this is the social security number of the one who made the transaction
$transaction->amount //this is the amount transfered
I need to compare the user-number to the user-numbers associated with orders in my database.
I could do that by making two arrays with those numbers开发者_如何学JAVA and use array_intersect
to find the numbers that are present in both the database and in the transactions xml.
Now i would have the user-numbers of the ones who have made a transaction but I still need to see if they transfered the correct amount.
I will be able to finish this but I somehow feel that there is a simpler solution by using the user-number as the index in the array and the amount as the value and create two arrays like that and compare them.
Can anyone help me here before I write some complicated, slow and messy code.
Thanks
Hopefully this will get you on the right track:
class Order
{
private $user = null;
private $amt = null;
public function __construct($user, $amt)
{
$this->user = $user;
$this->amt = $amt;
}
public function getUser()
{
return $this->user;
}
public function equals($transaction)
{
if ($this->user == $transaction->usernumber &&
$this->amt == $transaction->amount) {
return true;
}
return false;
}
}
// an empty array to hold the completed orders
$completed = array();
// loop over orders
foreach($orders as $order) {
// create a new order object
$order = new Order($order->user, $order->amount);
// loop over all transactions
foreach($transactions as $transaction) {
// if transactions is equal to our order then
// add it to the list of completed orders
if($order->equals($transaction)) {
$completed[] = $order;
}
}
}
This creates a small wrapper class for all your orders then allows you to compare an order to a transaction as your described it above.
I hope this helps.
精彩评论