I have an array which looks e.g. like this:
[
0=>['v'=> 1, 'c1'=>null , 'c2'=>null , 'c3'=>null],
1=>['v'=> 2, 'c1'=>'A' , 'c2'=>null , 'c3'=>null],
2=>['v'=> 3, 'c1'=>'A' , 'c2'=>'B' , 'c3'=>null],
3=>['v'=> 4, 'c1'=>'A' , 'c2'=>'B' , 'c3'=> 'C']
]
The keys c1, c2, c3 can be independently filled with either null or with any string.
I get a context input in the form of ['c1'=>'A','c2'=>'B','c3'=>'C'].
How do I determine the index of the row which best fits my input array.
"Best" means (in descending order):
- If there is an exact fit of all three strings (c1, c2, c3) in context and array return this index. (in this example, this rule would already apply and return 3)
- If this is not existing, return the index of the row where 2 context strings fit and the remaining one c* is null. (this would apply for a context like
['c1'=>'A', 'c2'=>'B', 'c3'=>'Z']and return 2) - If this is not existing , return the index of the row where
1 string fits and the two remaining c*s are null. (this would apply for a context like
['c1'=>'A', 'c2'=>'Y', 'c3'=>'Z']and return 1) - If this is not existing, return the index of the row where all three c*s are null. (this would apply for a context like
['c1'=>'X', 'c2'=>'Y', 'c3'=>'Z']and return 0)
I know that there can be entries in my array which theoretically return more than 1 index, but this can be excluded in the way the base array is created.
I started many tries which all ended up in a lot of spaghetti-nested-if-then-else stuff and it feels like there must be a more structural way to achieve this.