If you want an integer array in the end, why not just detect a string with is_binary and then parse it with a JSON library?
I smell a XY problem here, it might be better if you just tell us your desired result – and not ask how best to implement a single if condition.


















