Struggling to get my head around this one, and I find it quite tricky to explain so will do my best.
What I am trying to do is use a number to show allergens in foods. So for example an entry in the database could be "falafel" which has an allergen code of 16448 that is currently stored in the database.
What I need to do is take this number and convert it to hex, which gives me 4040.
I have this array of allergens
$allergens = array(
"celery" => array("label" => "Celery", "short" => "", "value" => 0x00000001),
"crustaceans" => array("label" => "Crustaceans", "short" => "", "value" => 0x00000002),
"eggs" => array("label" => "Eggs", "short" => "", "value" => 0x00000010),
"fish" => array("label" => "Fish", "short" => "", "value" => 0x00000020),
"gluten" => array("label" => "Gluten", "short" => "", "value" => 0x00000040),
"lupin" => array("label" => "Lupin", "short" => "", "value" => 0x00000080),
"milk" => array("label" => "Milk", "short" => "", "value" => 0x00000100),
"molluscs" => array("label" => "Molluscs", "short" => "", "value" => 0x00000200),
"mustard" => array("label" => "Mustard", "short" => "", "value" => 0x00000400),
"nuts" => array("label" => "Nuts", "short" => "", "value" => 0x00001000),
"peanuts" => array("label" => "Peanuts", "short" => "", "value" => 0x00002000),
"sesame" => array("label" => "Sesame", "short" => "", "value" => 0x00004000),
"soya" => array("label" => "Soya", "short" => "", "value" => 0x00008000),
"sulphur" => array("label" => "Sulphur", "short" => "", "value" => 0x00010000),
);
I then need to go through here, and see where 4040 matches. So in this case it would be gluten (40), and sesame (4000).
Another example would be a dish that has an allergens code of 320. Converted to hex this is 800. So I would need to return the labels for values of 400, 200, 100, 80, and 20 from the above array.
The numbers in the DB I cannot change. The array I posted above was in code that was previously used in the website that is no longer working.
Another example is something in the DB has 96 as it’s allergens code. Which in hex is 60, which can be made of 40 and 20 from the array which makes sense given this food. I know the values in the table are not in hex. But I need to convert them to hex, and then with this hex number find how it can be made from the numbers in the array and return the labels based on these numbers
How can I achieve this? Hopefully my explanation makes sense.
2
Answers
Found a way to make it work. I pass in the number from the DB and array of allergens to this function -
(@sunnyj58 In continuation from our discussion in comments)
The values in the table represent a bitmask as first alluded to by @shingo in comments.
The "bitmask system" revealed from your table uses 14 of the bits in a 16-bit integer (unsigned).
The allergen themselves are like checkboxes (bits).
Using this table as a guide I will present a solution which will intrinsically infer an algorithm. However I wont be using hexadecimal to present the solution, though you can later if you find it easier to work with.
See if you can transform this into code. I’ll have to come back to this answer to update it with code later. (out of time).