0

I have an array like the below which comes from my db. It holds currency pairs (eg. USD to AUD dollars, etc).

 Array
 (
[0] => Array
    (
        [xrate_amount] => 1
        [xrate_from] => AUD
        [xrate_to] => GBP
        [xrate_date] => 2020-12-05 01:50:29
        [xrate_rate] => 0.0000000000
    )

[1] => Array
    (
        [xrate_amount] => 1
        [xrate_from] => BRL
        [xrate_to] => AUD
        [xrate_date] => 2020-12-05 02:16:29
        [xrate_rate] => 0.0000000000
    )

[2] => Array
    (
        [xrate_amount] => 1
        [xrate_from] => JPY
        [xrate_to] => AUD
        [xrate_date] => 2020-05-12 00:00:00
        [xrate_rate] => 0.0139800000
    )

[3] => Array
    (
        [xrate_amount] => 1
        [xrate_from] => JPY
        [xrate_to] => CAD
        [xrate_date] => 2020-05-12 00:00:00
        [xrate_rate] => 0.0128100000

I am trying to insert a new currency pair into my db only if the currency pair doesn't already exist in the db. I am trying to write a function that receives $xrate_from and $xrate_to from a form and then search the above array called $inserted_currency (comes from db actually) and returns TRUE is the pair already exists (btw JPY CAD is different than CAD JPY).

I am totally stumped and would appreciated any guidance.

1
  • 1
    I would do that on the database level : adding a unique key on the column pair xrate_from, xrate_to. Then you can add items in it with INSERT IGNORE INTO or INSERT INTO ... ON DUPLICATE KEY UPDATE ... Commented Dec 5, 2020 at 3:41

3 Answers 3

2

First, you need to make two one-dimensional arrays for those columns that you need to compare in the end. Then find the keys of the two values in those arrays and make sure it's the same.

public function pairExists($yourXRateFromValueFromform, $yourXRateToValueFromform, $yourArray): bool
{
   return array_search($yourXRateFromValueFromform, array_column($yourArray, 'xrate_from')) === array_search($yourXRateToValueFromform, array_column($yourArray, 'xrate_to'))
}
Sign up to request clarification or add additional context in comments.

2 Comments

Add some description of how you do that
No problem on that it is much prettier now
1

You can use array_filter. Check below

<?php

function exchangeRateExists($allPairs, $xrate_from, $xrate_to) {
    return (bool)array_filter($allPairs, function($pair) use ($xrate_from, $xrate_to){
        return $pair['xrate_from'] == $xrate_from && $pair['xrate_to'] == $xrate_to;
    });
}

Comments

0
function existsxratepair($xrateFrom,$xrateTo, $array) {
    foreach ($array as $key => $val) {
        if ($val['xrate_from'] == $xrateFrom && $val['xrate_to'] == $xrateTo) {
            return TRUE;
        }else{
            return FALSE;
        }
    }

 }
 // call the function
 existsxratepair('JPY','CAD',$array);

with this code you can find the values are exists or not.

you can also check the values in database with use of query like

SELECT * FROM yourTableName WHERE xrate_from = 'yourValue' AND xrate_to = 'yourValue'

then count result if it return more then 0 row it exist in database

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.