Forum Moderators: coopster

Message Too Old, No Replies

Re-keying an array

         

technossomy

8:24 am on May 21, 2005 (gmt 0)

10+ Year Member



Dear all

Suppose I have an array:

$arr = array(0 => array("AAB", 10.0, 2000), 1 => array("ABC", 12.2, 3000));

I want to change this conveniently into

$arr = array("AAB" => array(10.0, 2000), "ABC" => array(12.2, 3000));

Of course one may assume that the strings in this array are all unique. Is there a standard way to do this other than to traverse the original array to populate the new array?

Best wishes

Tech

technossomy

11:39 am on May 21, 2005 (gmt 0)

10+ Year Member



Problem solved. In constructing the array, I say:

while ($row) $arr[$row['Symbol']] = $row;

where $row would contain my data, as opposed to:

while ($row) $arr[] = $row;

Anecdotal evidence suggests that this first method is slower than the second one but not substantially so.

jatar_k

4:52 pm on May 21, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



though even if it is lsower it is still faster than building the array and then having to rewalk the whole thing.