1

I have the following arrays named investmentprogramscriteria and companyInvestmentProfil:

Array
(
[0] => Array
    (
        [investmentprogramcriteriaID] => 20
        [investmentprogramID] => 21
        [criteriaID] => 59
        [criteriaIsMatchedIf] => 1
    )

[1] => Array
    (
        [investmentprogramcriteriaID] => 21
        [investmentprogramID] => 21
        [criteriaID] => 57
        [criteriaIsMatchedIf] => 1
    )

)


Array
(
[0] => Array
    (
        [investmentprofileID] => 1
        [companyID] => 27
        [criteriaID] => 54
        [investmentprofileCriteriaAnswer] => 1
    )

[1] => Array
    (
        [investmentprofileID] => 2
        [companyID] => 27
        [criteriaID] => 57
        [investmentprofileCriteriaAnswer] => 0
    )

[2] => Array
    (
        [investmentprofileID] => 3
        [companyID] => 27
        [criteriaID] => 58
        [investmentprofileCriteriaAnswer] => 0
    )

[3] => Array
    (
        [investmentprofileID] => 4
        [companyID] => 27
        [criteriaID] => 59
        [investmentprofileCriteriaAnswer] => 1
    )

)

If the last 2 KEYS AND THEIR VALUES from the first array are found in the second array, code should return match, else should return not match.

I make an attempt to resolve this with array functions, but nothing worked and now I am trying to solve it with php, but no luck so far.

I am trying with:

foreach ($investmentprogramscriteria as $programcriteria) {
    //foreach($companyInvestmentProfil as $profile) {
        if (($programcriteria['criteriaID'] == $companyInvestmentProfil[$i]['criteriaID']) && ($programcriteria['criteriaIsMatchedIf'] == $companyInvestmentProfil[$i]['investmentprofileCriteriaAnswer']))  {
        $match = "Match";                              
    } else {
       $match = "Not Match";
       continue;
// }                                
}

                        }

3 Answers 3

2

The best way is use foreach twice !!!

foreach ($investmentprogramscriteria  as $value) {
    foreach ($companyInvestmentProfil as $v) {
        if($v["criteriaID"] == $value["criteriaID"]) echo "match<br>";
        else echo "not Match<br>";
    }    
}
Sign up to request clarification or add additional context in comments.

2 Comments

Like this, I get bunch of match and / or not match, depending from the key. Even if one value is not matched, code should return not match. Any idea how to do that?
This will return a match for every $companyInvestmentProfil value that matches any $investmentprogramscriteria value. I think OP is after a 1-to-1 match
2

Update

Your code contains a bug:

$programcriteria['criteriaID'] == $companyInvestmentProfil[$i]['criteriaID']

the variable $i is never defined. You probably meant to write something like:

// assign keys to $i
foreach ($investmentprogramscriteria as $i => $programcriteria) {
}

In which case, either using the array_slice + array_keys approach below, with or without the array_intersect_assoc approach should work, if you use them on the $programcriteria and $companyInvestmentProfil[$k] arrays respectively.


What you're basically looking for is the array_intersect_assoc function. That function returns an array with all key-value pairs that are present in both arrays:

$intersect = array_intersect_assoc($array1, $array2);

To see how many key-value pairs match, just count($intersect);

To see if the last 2 keys of the first array matched, simply write this:

$lastKeys = array_slice(array_keys($array1), -2);
if (isset($intersect[$lastKeys[0]]) && isset($intersect[$lastKeys[1])) {
    return 'match';
}
return 'no match';

Check the manual on array_slice for details.

6 Comments

give me a moment to try this
$intersect didn't worked. However ,if you can notice, my keynames are not the same. Do you think that that may be the reason why intersect give wrong results?
Set up a fiddle with what you tried. I've used $array1 and $array2 as variable names, but I suspect what you need/want is $investmentprogramscriteria[0] and $companyInvestmentProfil[0] instead
@user2417624: I've also spotted an issue in the code you originally posted, updated my answer and added some suggestions on how to use the functions I mentioned correctly
I was googling where I can create fiddle, when I notice your second comment. The [$i] variable is setted elsewhere in the code, because there may be many investment programs array, so I was trying to use it, but it is the one that cause the issues. However, If I use [0] as you suggest, it will check only the first key-value par and then it will stop.
|
0
$array1 = array("a" => "greens", "reds", "blues", "red");
$array2 = array("b" => "green", "yellow", "red");
$result = array_intersect($array1, $array2);

 if(is_array($result)){
    echo 'match';
 }else{
    echo 'not match';
 }

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.