Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
150 views
in Technique[技术] by (71.8m points)

PHP non-sequential array keys. Hazards in data handling? Am I being too paranoid?

Scenario: fairly standard to start with I imagine, pulling data with numeric keys from databases and JSON submissions into arrays, storing to database and transmitting as JSON. However, because of the application, a fair amount of processing, comparison and merging can take place in between.

So, my fear is that in a substantial application doing lots of data processing, some great big multidimensional array of correctly keyed data (or a nested part of it) will get blasted into sequentially indexed array with the wrong keys by one or other operation. Been there and been bitten by that in the past, and developed the habit of prepending a letter to the keys to keep them as strings (since PHP will cast string representations of integers to ints). However, that's getting really messy in this application - I don't want to be iterating over huge datasets to fudge or unfudge foreign keys n levels into a nested structure.

I could alleviate all this concern by working with objects instead of MD arrays, but a) that's going to mean reworking a load of library which I don't much fancy; and b) more importantly because of what we're doing much of the time passing/copying by reference is a curse not a blessing, so we'd end up doing loads of deep copies with all the inefficiency that would entail.

I also get the impression that over the years that I've been religiously prepending letters to my keys while processing them, PHP (this project is currently on 7.2) has moved on substantially, non-sequential numeric keys are handled fairly safely, and for most if not all functions (e.g. array_merge) which would lose the keys, there are near-equivalents (e.g. array_replace) which won't. I have done a fair number of tests and it's almost like there isn't a problem... But, the more I read into this, the more contradictory information I find, and nothing very definitive.

The questions therefore become:-

  1. Am I being too paranoid?
  2. Is there actually no real risk of data corruption involved with non-sequential numeric keys so long as one avoids functions which are documented as not preserving keys?
  3. Is pretty much all functionality of built in array functions which don't preserve keys now provided by alternatives which do, or is there some fundamental thing or other left which can't be done?
  4. Are there any gotchas which aren't documented or which I'm failing to think of?
question from:https://stackoverflow.com/questions/66055388/php-non-sequential-array-keys-hazards-in-data-handling-am-i-being-too-paranoid

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...