All day long he had been working with a function which used “printf” commands to produce output from a database query. Simple and effective, but it meant that he was locked into one layout everytime he called that function, and if he needed the data displayed in another format, he would have to make another function altogether.
Too limiting, too limiting, he would rail in his thoughts, if only there was some way to populate an array with the db output, then just return the array at the end of the function and parse it after calling!
Simple in principle, yes, but he was working with multiple rows from a single query, which meant that the array to be returned would be nested at least two levels deep: one level for the row ID, to separate rows from each other, and then another level for the actual row data itself.
Nested arrays, he grumbled. I hate nested arrays. The less I have to deal with them, the better. He tried to visualize it, to see himself typing out the code which would loop through the row data and nest it within a loop of row ID’s, all nested within a plain old container variable. But he couldn’t do it. Somehow the vision would not come. He didn’t even need to see the syntax error message to know that this was not working.
It hit him later, in the subway station; he had been so obsessed with using foreach loops and muddling over whether to put variables in quotation marks, that he had missed the simpler solution right under his nose: There’s no need to use a loop for everything; just declare the associative indices explicitly on the second level! After a furtive glance around the empty station to make sure he was alone, he whipped out his computer and began typing furiously.
He was going to sleep well tonight. If he could only refrain from blogging…