This post is to share my PHP programming experience. If you are a PHP programmer you would have probably heard the built in function shuffle. As the name implies shuffle function takes an array and return an output array whose elements are randomized or shuffles. Here a sample program.
<?php
$numbers = range(1, 10);
echo 'Before shuffle' . "\n";
print_r($numbers);
srand((float)microtime() * 1000000);
shuffle($numbers);
echo 'After shuffle' . "\n";
print_r($numbers);
?>
The above code returns the following output
Before shuffle
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
[5] => 6
[6] => 7
[7] => 8
[8] => 9
[9] => 10
)
After shuffle
Array
(
[0] => 4
[1] => 2
[2] => 3
[3] => 7
[4] => 6
[5] => 5
[6] => 1
[7] => 8
[8] => 9
[9] => 10
)
The above code works pretty fine and produces the expected output. But the problem with shuffle method is, it fails to preserve the array keys, now consider this example
<?php
$numbers = array(
'a' => 'apple',
'b' => 'ball',
'c' => 'cat',
'd' => 'dog',
'e' => 'egg',
'f' => 'fish',
);
echo 'Before shuffle' . "\n";
print_r($numbers);
srand((float)microtime() * 1000000);
shuffle($numbers);
echo 'After shuffle' . "\n";
print_r($numbers);
?>
The above code generate the following output
Before shuffle
Array
(
[a] => apple
[b] => ball
[c] => cat
[d] => dog
[e] => egg
[f] => fish
)
After shuffle
Array
(
[0] => dog
[1] => egg
[2] => cat
[3] => fish
[4] => ball
[5] => apple
)
00ps the array keys has been broken after invoking shuffle method. Let us try my custom shuffle method
<?php
$numbers = array(
'a' => 'apple',
'b' => 'ball',
'c' => 'cat',
'd' => 'dog',
'e' => 'egg',
'f' => 'fish',
);
echo 'Before shuffle' . "\n";
print_r($numbers);
srand((float)microtime() * 1000000);
$numbers = custom_shuffle($numbers);
echo 'After shuffle' . "\n";
print_r($numbers);
function custom_shuffle($array = array()) {
$temp_array = array();
while($rand_key = array_rand($array)) {
$temp_array[$rand_key] = $array[$rand_key];
unset($array[$rand_key]);
}
return $temp_array;
}
?>
The above code generates an output as follow
Before shuffle
Array
(
[a] => apple
[b] => ball
[c] => cat
[d] => dog
[e] => egg
[f] => fish
)
After shuffle
Array
(
[f] => fish
[e] => egg
[b] => ball
[c] => cat
[d] => dog
[a] => apple
)
Hooray it works, if you think that this code can optimized feel free to post a comment.
Monday, May 25, 2009
PHP shuffle
Posted by
sivaji
at
5/25/2009 11:36:00 AM
0
comments
Labels: php, programming
Sunday, April 26, 2009
/me Got Selected For GSoC 2009
I am really excited to say that i got selected for Google Summer of Code (GSoC) 2009. I will be working with an active Open Source Community called *Drupal*. Drupal is an active participant of GSoC for last four year. Google has sponsored about $4,00,000 for Drupal project as of GSoC 2009. This year Google sponsors totally $90,000 for 18 amazing Drupal projects, my project is one among them. My project is all about enhancing an already existing quiz module started by webchick in gsoc 2005. More about my project here. Here my soc proposal
Thanks to ILUGC and Jayafossclub for motivating me. Also thanks to Matt Butcher who is a mentor for my GSoC project and Shyamala an active Chennai Drupal community member and local mentor for my GSoC project. I have lot to say about my mentors, will write it in next post. Also thanks Google, GSoC organizers especially LH, Drupal community and mentors who revived and approved my proposal.
This post will make no sense if i don't mention about my notorious GSoC friends ajuonline (also idiotonline sometimes :P) and Sup3rkiddo who took a great effort to organize gsoc meet and to spread the feel of GSoC among chennai.
Congrats to all the students who got through this year GSoC.
Lets code for cotton !! Happy Hacking !!
Posted by
sivaji
at
4/26/2009 05:38:00 AM
0
comments
Wednesday, April 01, 2009
Top 50 Image Galleries
A massive collection of image galleries to add more colors to your websites/blog
http://net.tutsplus.com/articles/web-roundups/50-excellent-image-galleries-you-can-use-today/
Posted by
sivaji
at
4/01/2009 04:27:00 AM
0
comments
Labels: jquery, php, website design
Sunday, March 22, 2009
Google summer of code 2009 meetup at chennai
There is a GSoC meetup planned where you can meet few summer of coders from the past and everyone who is planning to participate this year is welcome to share/exchange ideas and clear up any doubts they have regarding the program. Some of the students from the past years who will now be mentoring are also going to be present :)
So in case you happen to be in Chennai and can make it, please do :)
Date: Saturday, March 28, 2009 at 4:30:00 PM IST
Agenda: Discuss/Share/Talk/Have Fun aka Feel the Love of SoC :P
Venue: CCD, IIT Madras Campus.
Whom/How to contact:
Join the GSoC - Indian Community Mailing Lists:
http://groups.google.com/
Or catch any one of us at ##gsoc-india on irc.freenode.net
People who have confirmed so far:
Akarsh Simha - GSoC 2008 student for KDE/Kstars and GSoC 2009 Mentor
for the same :) IRC Nick - kstar
Arun Chaganty - GSoC 2008 student for Gnome, IRC Nick - vimzard
Sudharshan S - GSoC 2008 student for Openmoko, IRC Nick - Sup3rkiddo
Ajay Kumar - GSoC 2008 student for Sahana, IRC Nick - ajuonline
Sivaji - Going to apply for GSoC 2009 student for Drupal, IRC Nick - sivaji
Posted by
sivaji
at
3/22/2009 02:57:00 AM
0
comments
Labels: forward message, Google, gsoc
Friday, March 13, 2009
Ruby Array
A nice tutorial to learn about Ruby arrays
http://www.techotopia.com/index.php/Understanding_Ruby_Arrays
Posted by
sivaji
at
3/13/2009 05:04:00 AM
0
comments
Labels: programming, Ruby
Tuesday, February 24, 2009
jqModal
Another nifty Jquery plugin called jqModal to display popups and dialogue boxes, it has good documentation too looks very kool for me.
Go ahead and test it here http://dev.iceburg.net/jquery/jqModal/#examples
Posted by
sivaji
at
2/24/2009 05:04:00 AM
0
comments
Labels: jquery, themes, website design
