tags:

views:

47

answers:

3

I've come across an old app that uses an id to name type array, for example...

array(1) {
  [280]=>
  string(3) "abc"
}

Now I need to reorder these, and a var_dump() would make it appear that that isn't going to happen whilst the keys are integers.

If I add an a to every index, var_dump() will show double quotes around the key, my guess to show it is now a string (this sort of array is called a hash table in other languages I believe)...

array(1) {
  ["280a"]=>
  string(3) "abc"
}

This would let me easily reorder them, without having to touch more code.

Is there a way to force the keys to be strings, so I can reorder them without ruining the array?

Thanks!

Update

This does not work.

$newArray = array();
foreach($array as $key => $value) {
   $newArray[(string) $key] = $value;
}

A var_dump() still shows them as integer array indexes.

Update

OK, I've decided not to wait this out and have changed how this works to something like this...

array(1) {
  [0]=>
  array(2) {
     ["name"] => string(3) "abc"
     ["id"]   => int(280)
  }
}

Then I had to modify some more code, to suit.

What I'm asking may be impossible, but I will leave this question here as someone may have a solution.

+1  A: 

EDIT:

I assumed that if they are integers, I can't reorder them without changing the key (which is significant in this example). However, if they were strings, I can reorder them how they like as the index shouldn't be interpreted to have any special meaning. Anyway, see my question update for how I did it (I went down a different route).

Actually they dont have to be in numeric order...

array(208=>'a', 0=> 'b', 99=>'c');

Is perfectly valid if youre assigning them manually. Though i agree the integer keys might be misinterpreted as having a sequential meaning by someone although you would think if they were in a non-numeric order it would be evident they werent. That said i think since you had the leeway to change the code as you updated that is the better approach.


Probably not the most efficient way but easy as pie:

$keys = array_keys($data);

$values = array_values($data);
$stringKeys = array_map($keys, 'strval');

$data = array_combine($stringKeys, $values);

//sort your data
prodigitalson
`array_map` arguments are around the wrong way. Trying it out now, thanks.
alex
Sorry, I didn't mention it, but this is on a PHP4 project so no `array_combine()`. I'll just iterate through and build it.
alex
Bummer, didn't work. They seem to get cast back to integer. I even tried using the cast operator `(string)`. It is not until I add a non integer that they turn to string.
alex
Sorry about the argument order faux pas.
prodigitalson
Thanks for the update, I didn't realise I could reorder a numeric keyed array.
alex
No problem... I wish i would have mentioned it sooner.. i might have saved you some time :-)
prodigitalson
+2  A: 

how about this?

$newArray = array();
foreach($oldArray as $key => $value) {
   $newArray[$key.''] = $value;
}

edit: tried that too and yes, it didn't work.

From the PHP docs:

A key may be either an integer or a string. If a key is the standard representation of an integer, it will be interpreted as such (i.e. "8" will be interpreted as 8, while "08" will be interpreted as "08"). Floats in key are truncated to integer. There are no different indexed and associative array types in PHP; there is only one array type, which can both contain integer and string indices.

I guess now it depends on how do you want to sort the array. (final output?)

andyk
Unfortunately, I've [tried that](http://codepad.org/1IqwfZUx) and it doesn't work. Thanks for stopping by Andy!
alex
yes how do you want to sort the array exactly?
prodigitalson
@prodigitalson I assumed that if they are integers, I can't reorder them without changing the key (which is significant in this example). However, if they were strings, I can reorder them how they like as the index shouldn't be interpreted to have any special meaning. Anyway, see my question update for how I did it (I went down a different route).
alex
Ok, this is probably too old already and might not be helpful to you now. (not a SO frequenter now, sorry) But have you looked at `asort()` ?
andyk
A: 

Edit: This should work

foreach($array as $key => $value) { 
    $newkey = sprintf('%s',$key);
    $newArray["'$newkey'"] = $value; 
} 
Interesting, but this just adds more complexity I believe.
alex
Have a look at the edit...I think this should work.
Actually now that I think about this, even doing this might work: $newArray["'$key'"] =$value
im going to term this the "nuclear option"
prodigitalson
It works if you want array indexes wrapped in single quotes, which I don't. I guess I might be asking the impossible. Thanks for your answer anyway.
alex