0

Let's say I have two arrays...

$radicand_array = array(3, 5, 5, 2);
$coeff_array = array(-10, 14, 3, -6);

I'd like to be able to determine which (if any) values in $radicand_array match, AND also know what the keys are. So, in this case I would want to know that there's a match on keys 1 and 2 of $radicand_array.

I need to know this because I want to then add the corresponding key values in $coeff_array. So, in this case I would then add 14 and 3 based on the matching 5's in $radicand_array.

I've tried array_count_values(), but it doesn't seem to give the key values like I want. Is there a PHP function ready made for this?

4
  • Please, add desired output of given example data. Commented Mar 10, 2015 at 2:17
  • Here appears a promising answer: stackoverflow.com/questions/6460993/… Commented Mar 10, 2015 at 2:17
  • Can you tell me what you expect as finel result of this?? Commented Mar 10, 2015 at 2:28
  • As explained in the post, I would expect the output to tell me that keys 1 and 2 match in $radicand_array. If $radicand_array was array(3, 5, 5, 2, 5), then I would expect the output to tell me that keys 1, 2, and 4 match. Commented Mar 10, 2015 at 2:44

2 Answers 2

0

Using the link:

Fixed. Good man!

<?php
    // $radicand_array = array(3, 5, 5, 2);
    $radicand_array = array(3, 5, 5, 2, 3, 3);
    $coeff_array = array(-10, 14, 3, -6);

    $unique = array_unique($radicand_array);
    $duplicates = array_diff_assoc($radicand_array, $unique);

    // Duplicate keys
    $aDupes = array_keys(array_intersect($radicand_array, $duplicates));
    $iSum = 0;
    $iCountDupes = count( $aDupes );
    for( $i = 0; $i < $iCountDupes; ++$i )
    {
        if( !empty( $coeff_array[ $aDupes[ $i ] ] ) )
        {
            $iSum = $iSum + $coeff_array[ $aDupes[ $i ] ];
        }
    }
    var_dump( $iSum );
?>
Sign up to request clarification or add additional context in comments.

4 Comments

almost there, use those keys in the second array and get the sum
I just found an issue. This seems to not work if $radicand_array were like this: $radicand_array = array(3, 5, 5, 2, 3, 3);. In other words, if there are multiple matches. Does that completely change things?
Fixed. Needs an empty check if the coeff array has less values.
I should have been more specific. I'm assuming that $coeff_array has a matching number of values. I believe I've got it though, so I'll be posting that as an answer in a moment. Credit to Vladimir Ramik though.
0

This is an edited version of Vladimir Ramik's post to handle multiple sets of matches...

$radicand_array = array(3, 5, 5, 2, 3);
$coeff_array = array(-10, 14, 3, -6, 4);

$unique = array_unique($radicand_array);
$duplicates = array_diff_assoc($radicand_array, $unique);

$new_array = array_chunk($duplicates, 1);
// Duplicate keys
$aDupes = array_keys(array_intersect($radicand_array, $new_array[0]));

$iCountDupes = count( $aDupes );
        for($i = 0; $i < $iCountDupes; ++$i){
            $ans_coeff_1 += $coeff_array[$aDupes[ $i ]];
        }

        echo $ans_coeff_1;

// Duplicate keys
$bDupes = array_keys(array_intersect($radicand_array, $new_array[1]));

$iCountDupes = count( $bDupes );
        for($i = 0; $i < $iCountDupes; ++$i){
            $ans_coeff_2 += $coeff_array[$bDupes[ $i ]];
        }

        echo '<br>' . $ans_coeff_2;

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.