开发者

PHP - how many members of an array are present?

开发者 https://www.devze.com 2023-02-09 11:52 出处:网络
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

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?

开发者_C百科

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

Many thanks, Paco


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


See about array_intersect()

0

精彩评论

暂无评论...
验证码 换一张
取 消