1

Is it possible to perform changes to the "structure" of an array of associative maps (bulk renaming keys or dropping them entirely) without linearly iterating over the entire array and copying information to a new array? Is there another PHP data structure, like a table or something, that supports this?

To make this a bit clearer, can I take this:

$arr = array(
    array("id" => 1, "something" => "etc"),
    array("id" => 2, "something" => "etc"),
    array("id" => 3, "something" => "etc"),
    array("id" => 4, "something" => "etc"),
    array("id" => 5, "something" => "etc"),
    ...
    array("id" => $over9000, "something" => "etc")
);

and turn it into this:

$newarr = array(
    array("id" => 1, "different" => "etc"),
    array("id" => 2, "different" => "etc"),
    array("id" => 3, "different" => "etc"),
    array("id" => 4, "different" => "etc"),
    array("id" => 5, "different" => "etc"),
    ...
    array("id" => $over9000, "different" => "etc")
);

WITHOUT doing this:

$newarr = array_map(function($i){
    return array(
        "id" => $i["id"], 
        "different" => $i["something"]
    );
}, $arr);

My use case is getting about 70,000 rows from an SQL database in some format I can't control, and having to serialize it to JSON a particular way (the keys are differently named). It seems like a huge waste of 10 to 15 seconds to just loop through the array of all the record objects returned and perform the same selective copying operation on each one.

Is there a way to do this in constant time? I'm interfacing with the PHPADODb to get stuff from the database, so if it already has some structure like this which separates schema from data that would be perfect.

Asad Saeeduddin
  • 46,193
  • 6
  • 90
  • 139
  • Possible duplicate of http://stackoverflow.com/questions/2212948/php-rename-array-keys-in-multidimensional-array Any chance that answers your question? – jpycoder Feb 19 '14 at 03:47
  • @jpycoder I read that, and tried to emphasize in the title that I'm **not** looking for a solution that involves iterating over the array. That's the whole point of this question. – Asad Saeeduddin Feb 19 '14 at 03:49
  • 1
    any internal function would still be looping through the array –  Feb 19 '14 at 03:49
  • 2
    70k items array shouldn't be iterated for 10 seconds – zerkms Feb 19 '14 at 03:50
  • @Dagon This is fundamentally not a linear time operation, it is only so because of implementation details (the array type I'm using in this case). Renaming a table column is O(1). – Asad Saeeduddin Feb 19 '14 at 03:50
  • @Asad: "This is fundamentally not a linear time operation" --- any reasons for it to be not linear? – zerkms Feb 19 '14 at 03:51
  • @zerkms SLow connection, VM on my old laptop, exaggeration (I haven't actually timed it). – Asad Saeeduddin Feb 19 '14 at 03:51
  • @Asad: slow connection to **WHAT**? You've asked about working with array. Array is stored in a process memory. – zerkms Feb 19 '14 at 03:51
  • 4
    Btw, cannot you change the sql query to return what you need, not some rubbish then? – zerkms Feb 19 '14 at 03:52
  • If you need to serialize it to JSON and don't want to iterate the array, what about performing a string replacement on the resultant JSON? (which would normally be the last thing I'd recommend) – Michael Berkowski Feb 19 '14 at 03:52
  • @zerkms The database. I'm loading a page and it takes around 10 seconds. The point isn't the exact time here. – Asad Saeeduddin Feb 19 '14 at 03:52
  • @Asad: if it takes 10 seconds to load with `something` it will be exactly the same time for doing so with `different` column name. What's wrong with changing sql query or just modifying an array? – zerkms Feb 19 '14 at 03:53
  • @MichaelBerkowski I could try that, but I don't think I can come up with a safe enough regex to avoid mangling any of the actual data. Also, I don't know whether that would be faster or slower. – Asad Saeeduddin Feb 19 '14 at 03:54
  • Slower? You mentioned that performance isn't the subject here. – zerkms Feb 19 '14 at 03:54
  • @zerkms Yes, I was responding to your earlier question in capslock. – Asad Saeeduddin Feb 19 '14 at 03:54
  • Regex may or may not be necessary depending on your data. The way to see if it is faster or slower is to _try it and time it_. – Michael Berkowski Feb 19 '14 at 03:55
  • @zerkms No, I didn't mention this. – Asad Saeeduddin Feb 19 '14 at 03:55
  • "The point isn't the exact time here". So the point is either the performance or not? Could you finally decide it please? – zerkms Feb 19 '14 at 03:55
  • @zerkms Maybe I should have written "The point isn't the **exact** time here". Whether or not it was 10 seconds or 6 seconds is irrelevant. – Asad Saeeduddin Feb 19 '14 at 03:56
  • @Asad: it won't be even 6 seconds to iterate over 70k items array. Have you tried to measure how long the array iteration takes or just think your guessing skills are good enough today? – zerkms Feb 19 '14 at 03:56
  • @Asad: so the issue is solved? – zerkms Feb 19 '14 at 03:57
  • As far as other php data structures go, there aren't any. At least none that I know of, and none that will work faster than PHP's built in arrays. The best way to do what you need would involve how you pull down the data from the DB, without more description on how you do that we can't help you easily. – John V. Feb 19 '14 at 04:02
  • 1
    Would be nice if there was a json output filter you could use for this :) – Ja͢ck Feb 19 '14 at 04:22

1 Answers1

0

Is there a way to do this in constant time?

There is no way to do that. The items in an array are independent, so there is just no way to change their structure in a constant time.

zerkms
  • 249,484
  • 69
  • 436
  • 539