0

I have the following arrays named investmentprogramscriteria and companyInvestmentProfil:

Array
(
[0] => Array
    (
        [criteriaID] => 55
        [criteriaIsMatchedIf] => 11, 12, 13
    )

[1] => Array
    (
        [criteriaID] => 54
        [criteriaIsMatchedIf] => 1
    )

[2] => Array
    (
        [criteriaID] => 52
        [criteriaIsMatchedIf] => 1
    )

)


Array
(
[0] => Array
    (
        [criteriaID] => 52
        [investmentprofileCriteriaAnswer] => 1
    )

[1] => Array
    (
        [criteriaID] => 54
        [investmentprofileCriteriaAnswer] => 1
    )

[2] => Array
    (
        [criteriaID] => 58
        [investmentprofileCriteriaAnswer] => 0
    )

[3] => Array
    (
        [criteriaID] => 59
        [investmentprofileCriteriaAnswer] => 1
    )

[4] => Array
    (
        [criteriaID] => 55
        [investmentprofileCriteriaAnswer] => 1
    )

)

I am trying to find out if the value of the criteriaID from the first array (investmentprogramscriteria ) exists in the second array (companyInvestmentProfil) AND IF the value of the key criteriaIsMatchedIf from the first array is equal to the value of the key investmentprofileCriteriaAnswer from the second array.

My current php code returns wrong result at this time:

if (array_intersect($investmentprogramscriteria,$companyInvestmentProfil))     {
    echo "Match";
 } else {
    echo "Not match";
 }
5
  • Are the criteriaID unique in each array? Commented Aug 29, 2016 at 21:45
  • 1
    @AbraCadaver Yes, they should be unique Commented Aug 29, 2016 at 21:47
  • array_intersect will do a string comparison of the values to see if they are the same. Your values are an array, not a string. It won't recursively go through all levels of an array and compare all keys/values to see what is the same. Commented Aug 29, 2016 at 21:49
  • @JonathanKuhn You are right...Any idea how to check if values of the array keys are same as described above? Commented Aug 29, 2016 at 21:50
  • 1
    If the criteria id is unique, I personally would just index the second array with the criteria id when building it (or loop over and re-build it in that format). Then you can just loop over the first array, check if the criteria id key exists, check the other value and if they match save it into another array. Commented Aug 29, 2016 at 22:05

2 Answers 2

1

array_column() can extract the column indicated as a single dimension and indexes it by the next column indicated. Do this for both arrays and then use array_intersect_assoc() to check key and value:

if(array_intersect_assoc(
array_column($investmentprogramscriteria, 'criteriaIsMatchedIf', 'criteriaID'),
array_column($companyInvestmentProfil, 'investmentprofileCriteriaAnswer', 'criteriaID'))) {
    echo "Match";
} else {
    echo "No Match";
}

PHP >= 5.5.0 needed for array_column() or use the PHP Implementation of array_column().

Sign up to request clarification or add additional context in comments.

10 Comments

I got Fatal error: Call to undefined function array_column() . I am using PHP 5.2.4, I guess that is the reason...Any work arround?
Holy crap! That is no bueno. You should upgrade. I edited an alternate.
unfortunately anonymous functions were also added in php 5.3. So looks like you also have to define a function and pass the name by string.
@JonathanKuhn: Well damnit. I thought we were past all this crap!
@AbraCadaver, I get Parse error: syntax error, unexpected T_FUNCTION, expecting ')' in.... I guess that the reason for this is mentioned in the comment of the Jonathan Kuhn
|
1

You can try the code with array_map:

$elements1 = array();
foreach($investmentprogramscriteria as $item) {
    $elements1[] = $item['criteriaID'] . $item['criteriaIsMatchedIf'];
}
$elements2 = array();
foreach($companyInvestmentProfil as $item) {
    $elements2[] = $item['criteriaID'] . $item['criteriaIsMatchedIf'];
}

if (array_intersect($elements1, $elements2))     {
    echo "Match";
} else {
    echo "Not match";
}

5 Comments

They are looking for a match on criteriaID AND the other column.
@Andrej Ludinovskov, I am gettingt the following error Parse error: syntax error, unexpected T_FUNCTION, expecting ')' in .... I am using PHP 5.2.4, and upgrading is not an option at this time. Any work arround?
@Andrej Ludinovskov I get an error in line $elements1 = []; Parse error: syntax error, unexpected '['
Sure, it's too old PHP that doesn't have short syntax for array.
@Andrej Ludinovskov I get error Message: Undefined index: criteriaIsMatchedIf... Any idea why is that?

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.