A problem I encountered recently was, after a short search on the internet, easy to fix.
I was trying to arrange an array in php by it’s keys, but I didn’t know how at first. This is my array
$ray = array('2' => 'second', '1' => 'first', 'a' => 'alpha);
As you noticed the order is 2 – 1 – a. But i want to arrange it to 1 – 2 – a. The only thing I had to do was this:
ksort($ray);
Thats all you need to do. And it will arrange the array by key.
Something i recently discovered, and already regret I didn’t knew this before, makes it much more easier to parse $_POST or $_GET variables into ordinary variables so i can use it properly for mysql insertions.
This is what i used to do:
<?php $var1 = mysql_real_escape($_POST['var1']); $var2 = mysql_real_escape($_POST['var2']); $var3 = mysql_real_escape($_POST['var3']); ?>
Ofcourse we all can see the repeat in this, but i never figured out how to solve that issue.
But ofcourse there is always someone else with a better idea, and I like to share it with you
<?php foreach ($_POST as $key => $value){ $$key = mysql_real_escape_string($value); } ?>
And thats simply all. It will put all the $_POST array items into a numerous variables.
The double “$” does what you expect it to do, it makes a variable out of the content of the variable.
When $a = test, than $$a = test2 will result into $test = test2.
It is that easy!


