Posted by: phillipnb | December 2, 2011

sort() and asort()


A new php developer was asked to sort all the accounts in a bank accounts drop down of a php form. The drop down was populated using an array which pulled data from a table in the database. The developer enthusiastically took the task and dived into it immediately. After an hour the developer reported to his manager that he had completed the job. The code was moved to the test environment and from there to the production environment. With in a few minutes manager received a call from a concerned customer saying that the submitted form was giving strange errors, like ‘bank account was not found’. The manager immediately took the checked-in version of the code offline from the production and examined it. The problem was quickly found and rectified. The manager and the team were relieved.

Honestly, what went wrong here?. The form drop down array had bank account identification numbers for each customer who had multiple bank accounts. The indices of the array were account identification numbers and the corresponding account names were shown as the text in the drop down visible to the user.

For example, for a customer called Joe, the final drop down should have looked something like this:

<select name='bankacct'>
<option value='a213'>Savings Account - Joe</option>
<option value='b112'>Credit Account - Joe</option>
<option value='b200'>Current Account - Joe</option>
</select>

But, after sorting the array that was populating this drop down the new drop down looked like this:

<select name='bankacct'>
<option value='0'>Savings Account - Joe</option>
<option value='1'>Credit Account - Joe</option>
<option value='2'>Current Account - Joe</option>
</select>

Now, you know why angry customers called the customer service. Instead of using asort(), our developer used just the ‘sort()’ function of the php which reindexed the array removing all the old indexes which were account numbers. if you read the documentation of this sort() in the php manual, the manual cleary states that “This function assigns new keys to the elements in array. It will remove any existing keys that may have been assigned, rather than just reordering the keys.”. whereas for asort() the manual says that “This function sorts an array such that array indices maintain their correlation with the array elements they are associated with. This is used mainly when sorting associative arrays where the actual element order is significant.”. Without giving importance to these statements, the developer accidentally sorted the array using sort() function.

Moral of this story – Always be careful while you use these built-in php functions especially ones dealing with sort() and arrays.

Till next time, happy php programming! – Have a great Christmas!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: