1

Lets say this is my sql database column values:

Result  -      Reference

  1                A1
  2                A2
  3                A3
  4                A4
  5                A5

After fetching the above columns, I have the following arrays:

$inputValue = array(1,3,5);  //This is the User Input

$result = array(1,2,3,4,5); //This is the mysql fetched result of 'Results' column

$reference = array('A1','A2','A3','A4','A5'); //This is the fetched result of 'Reference' column

$filteredData = array_intersect($inputValue, $result);

print_r($filteredData);

When I do array_intersect($inputValue, $result); this gives the output:

Array ( [0] => 1 
    [1] => 3 
    [2] => 5 )

Now after the array_intersect, how will I store the corresponding references into another array? Meaning, after the intersect, the $filteredData has the array values 1,3,5. Now I also want the corresponding references to be stored in a separate array which should have this value: $MatchingReference = array(A1,A3,A5);

How is this done?

0

3 Answers 3

2

I would try this:

$filteredReferences = array();
foreach($filteredData as $key) {
    $filteredReferences[] = $reference[$key-1];
}
4
  • I like your suggestion. But when I tried that, I am getting: Notice: Undefined offset: 5 in C:\Users\test.php on line 18 Array ( [0] => A2 [1] => A4 [2] => ). My line 18 is $filteredReferences[] = $reference[$key];
    – Neel
    Jan 17, 2014 at 22:06
  • 2
    Remember that array indexes start at zero, not 1. Jan 17, 2014 at 22:06
  • Just 1 typo. You need to add the semi-colon (;) at the end of 1st line. Tried editing it but it says edit needs to be atleast 6 characters. Cheers.
    – Neel
    Jan 17, 2014 at 22:12
  • Hi Xardas, I did a few testing, but I think @BillKarwin approach is probably better. Your code works well with the example I had given where the array values are in order like 1,2,3,etc.. But if I have $inputValue as say '1,58,51' and $result array value as '1,2,58,4,51', then I will get an error since it is looking for $reference[$key-1].
    – Neel
    Jan 17, 2014 at 22:23
1

Combine the two arrays from the database into an associative array. Then use the user input to select only a subset of keys.

$filteredData = array_intersect_key(
    array_combine($result, $reference), 
    array_flip($inputValue)
);
2
  • Your method works too. Both yours and Xardas suggestion does exactly what I wanted!. Fantastic :)
    – Neel
    Jan 17, 2014 at 22:09
  • After a few testing, I think your method is the best approach. I had added my reason in the comment under @Xardas suggested answer. :) Thank you Bill.
    – Neel
    Jan 17, 2014 at 22:25
1

If this was a task in my own application, I would definitely implement the data filter with SQL instead of PHP.

Otherwise, you going to need perform a few manipulations to enjoy efficient key-based filtration and reach your desired output. (Demo)

var_export(
    array_intersect_key(
        array_column($resultSet, 'reference', 'result'),
        array_flip($inputValue)
    )
);
1
  • Thank you @mickmackusa I am about to update the codes in my app this year as an upgrade process and I will review the above code and try your recommendation. thank you for sharing your suggestion.
    – Neel
    Feb 23, 2022 at 3:20

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.