Paco
Paco

Reputation: 21

PHP - how many members of an array are present?

I have a set of 7 variables. Call them A, B, C, D, E, F, and G I need to detect how many out of the seven are present in any given row of a table that may contain anywhere from 0 to 7 of the variables.

The other twist is that each variable may have one of 6 "valid" values (call the values X, Y, X, AA, BB, CC) or be empty.

What I want to know is: In a given row, how many of the columns are populated with a valid value?

I have been scratching my head and can't seem to come up with a reliable algorithm. Any suggestions?

Many thanks, Paco

Upvotes: 2

Views: 114

Answers (2)

Alex Pliutau
Alex Pliutau

Reputation: 21957

See about array_intersect()

Upvotes: 0

Distdev
Distdev

Reputation: 2312

$vars = array('A', 'B', 'C', 'D', 'E', 'F', 'G');
$presentInDBRow = array_intersect($vars, $rowFromDatabase); //calculate intersection of arrays
count($presentInDBRow); // get count of instersection

Upvotes: 6

Related Questions