1

How can I tell if a php object is rather a sequentially iterable object or an associative iterable object?

I would like to distinguish in the same way json_encode does: sequential objects are rendered as "[...]" while associative objects are rendered as "{...}". In particular a cakephp Query object should be detected as sequential.

Emanuele Paolini
  • 9,912
  • 3
  • 38
  • 64
  • Not sure if https://stackoverflow.com/questions/173400/how-to-check-if-php-array-is-associative-or-sequential answers your question. – Nigel Ren Oct 04 '20 at 17:35
  • Thanks @Nigel Ren, I know that answer. A possible solution is to check the keys are a range 0...(n-1) but this is very ugly for a Query object. It might be a very expensive check. – Emanuele Paolini Oct 05 '20 at 05:54
  • I think the principle of linking to the other answer is that it provides what has so far been found as possible solutions. If there is anything that can do it, then the other question will most likely have the answer. – Nigel Ren Oct 05 '20 at 06:08
  • If you want to see how `json_encode()` works - https://github.com/php/php-src/blob/master/ext/json/json_encoder.c, probably php_json_determine_array_type(). – Nigel Ren Oct 05 '20 at 06:14
  • @NigelRen thank you so much for the reference to json_encode C source code: this is exactly what I would like to do... unfortunately I don't understand if it is possible to access that C function from php code. – Emanuele Paolini Oct 05 '20 at 15:35

0 Answers0