Recently I posted an article on how to compare two doctrine objects @ which works well in most cases, but ran into an issue yesterday where floats as strings were being set to 0 due to the (int) cast. This was a problem because on the website, I store ingredient portions in a float (decimal) format, which means that my object comparisons were not being evaluated correctly if I changed the ingredient portions from .5 to .66 as they would both be cast to int with a value of 0.

So I was working on a possible solution and came across a great little hack in the comments section of the docs for the is_numeric function @, in the comments, a user (, pointed out that you can add ‘+0′ to a string which evaluated to true using the is_numeric function and it will correctly cast the value of the string to the proper datatype, which worked well in converting string floats to float floats. So the revised code would look like:

$objectArray1 = array_map(function($value) { return (is_numeric($value)) ? ($value + 0): $value; }, $object1->toArray(false));