Posts Tagged ‘array’

Php:sizeof vs count

Monday, July 14th, 2008

First up are the sizeof and count functions. They can both be used to count the number of items in an array but does one do it better?

sizeof vs count
sizeof: 3.75928902626 seconds
count: 3.33035206795 seconds
Time saved: 0.428936958313 seconds; 12.8796280262%

The evidence says yes. The count function was over 12% faster in this test. Both functions are fast though taking 3-4 microseconds to count an array with 100,000 items. You might think it isn’t worth it but remember count is also a character shorter. Not only is it faster to run but it is also faster to type!

PHP:Arrange an array by the first letter

Monday, June 30th, 2008

Here is a small PHP tips&tricks post : we have an array full of values, let’s say a country list (in this example the list is limited, we don’t want to see 190+ values).
PHP  CODE:

Array
(
[0] => Array
(
[0] => Afghanistan
[name] => Afghanistan
[1] => AF
[code] => AF
)

[1] => Array
(
[0] => Albania
[name] => Albania
[1] => AL
[code] => AL
)

[2] => Array
(
[0] => Algeria
[name] => Algeria
[1] => DZ
[code] => DZ
)

[3] => Array
(
[0] => Bangladesh
[name] => Bangladesh
[1] => BD
[code] => BD
)

[4] => Array
(
[0] => Barbados
[name] => Barbados
[1] => BB
[code] => BB
)

[5] => Array
(
[0] => Belgium
[name] => Belgium
[1] => BE
[code] => BE
)

[6] => Array
(
[0] => Brazil
[name] => Brazil
[1] => BR
[code] => BR
)

[7] => Array
(
[0] => Cape Verde
[name] => Cape Verde
[1] => CV
[code] => CV
)

[8] => Array
(
[0] => Cayman Islands
[name] => Cayman Islands
[1] => KY
[code] => KY
)
)

PHP CODE:

Array

(

[0] => Array

(

[0] => Afghanistan

[name] => Afghanistan

[1] => AF

[code] => AF

)

[1] => Array

(

[0] => Albania
[name] => Albania

[1] => AL

[code] => AL

)

[2] => Array

(

[0] => Algeria

[name] => Algeria

[1] => DZ

[code] => DZ

)

[3] => Array

(

[0] => Bangladesh

[name] => Bangladesh

[1] => BD

[code] => BD

)

[4] => Array

(

[0] => Barbados

[name] => Barbados

[1] => BB
[code] => BB

)

[5] => Array

(

[0] => Belgium

[name] => Belgium

[1] => BE

[code] => BE

)

[6] => Array

(

[0] => Brazil

[name] => Brazil

[1] => BR

[code] => BR

)

[7] => Array

(

[0] => Cape Verde

[name] => Cape Verde

[1] => CV

[code] => CV

)

[8] => Array

(

[0] => Cayman Islands

[name] => Cayman Islands

[1] => KY

[code] => KY

)

)

and we want to display like this
-A-

* Afghanistan
* Albania
* Algeria

-B-

* Bangladesh
* Barbados
* Belgium
* Brazil

-C-

* Cape Verde
* Cayman Islands

, the right and easy way to do this would be to retain in a variable the first letter of the last country and in another variable the first letter of the current country. We compare them and if they are different we just output the first letter and then the name of the current country. All this in just few and simple to understand lines, right? :P
PHP  CODE:

//try to do the A-Z list
$v = function to get your country list from a DB sorted by country name!;
$last_letter = ”;
$current_letter = ”;
for ($i=0;$i
” . $current_letter . ”

“;
}
print $v[$i]["name"] . “”;
$last_letter = $current_letter;
}

PHP CODE:

//try to do the A-Z list

$v = function to get your country list from a DB sorted by country name!;

$last_letter = ”;

$current_letter = ”;

for ($i=0;$i<count($v);$i++)

{

$current_letter = substr($v[$i]["name"],0,1);

if ($last_letter != $current_letter)

{

print “<div align=center>” . $current_letter . “</div>”;

}

print $v[$i]["name"] . “<br />”;

$last_letter = $current_letter;

}

how to remove empty element from array

Monday, June 30th, 2008

$cleaned_array = array_filter($countries);
by not supplying a callback function to array_filter function it skips the empty values

Remove empty strings/elements from a PHP array

Monday, June 30th, 2008

<?php
$data = array(
‘  ‘,
‘test’,
123,
);
// filtering non-empty elements.
$data = preg_grep(’#S#’, array_map(’trim’, $data));
var_dump($data);
/* Result
array(2) {
[1]=>
string(4) “test”
[2]=>
string(3) “123″
}
*/
?>

PHP:How to disable PHP magic quotes effect

Monday, June 30th, 2008

set_magic_quotes_runtime(FALSE);
if (get_magic_quotes_gpc()) {
/*
All these global variables are slash-encoded by default,
because    magic_quotes_gpc is set by default!
(And magic_quotes_gpc affects more than just $_GET, $_POST, and $_COOKIE)
*/
$_SERVER = stripslashes_array($_SERVER);
$_GET = stripslashes_array($_GET);
$_POST = stripslashes_array($_POST);
$_COOKIE = stripslashes_array($_COOKIE);
$_FILES = stripslashes_array($_FILES);
$_ENV = stripslashes_array($_ENV);
$_REQUEST = stripslashes_array($_REQUEST);
$HTTP_SERVER_VARS = stripslashes_array($HTTP_SERVER_VARS);
$HTTP_GET_VARS = stripslashes_array($HTTP_GET_VARS);
$HTTP_POST_VARS = stripslashes_array($HTTP_POST_VARS);
$HTTP_COOKIE_VARS = stripslashes_array($HTTP_COOKIE_VARS);
$HTTP_POST_FILES = stripslashes_array($HTTP_POST_FILES);
$HTTP_ENV_VARS = stripslashes_array($HTTP_ENV_VARS);
if (isset($_SESSION)) {    #These are unconfirmed (?)
$_SESSION = stripslashes_array($_SESSION, ”);
$HTTP_SESSION_VARS = stripslashes_array($HTTP_SESSION_VARS, ”);
}
/*
The $GLOBALS array is also slash-encoded, but when all the above are
changed, $GLOBALS is updated to reflect those changes.  (Therefore
$GLOBALS should never be modified directly).  $GLOBALS also contains
infinite recursion, so it’s dangerous…
*/
}
function stripslashes_array($data) {
if (is_array($data)){
foreach ($data as $key => $value){
$data[$key] = stripslashes_array($value);
}
return $data;
}else{
return stripslashes($data);
}
}

PHP:array change key case

Monday, June 30th, 2008

<?php
$input_array = array(”FirSt” => 1, “SecOnd” => 4);
print_r(array_change_key_case($input_array, CASE_UPPER));
?>
The above example will output:
copy to clipboard
Array
(
[FIRST] => 1
[SECOND] => 4
)

PHP:get/retrieve file basename,extension,path,dirname

Monday, June 30th, 2008

$file = ‘/dir/some.gif’;
var_dump( pathinfo( $file ) );
Result:
array(3) {
["dirname"]=>
string(4) “/dir”
["basename"]=>
string(8) “some.gif”
["extension"]=>
string(3) “gif”
}
// or
$file_ext = substr($file, strrpos($file,”.”)+1);

Displaying Page Loading Time (Steps and Sample Code)

Saturday, June 28th, 2008

Here is how to display your page’s loading time:

1. Use the function microtime() to get the time in micro-seconds
2. Use the explode() function to turn the micro-time into an array.
3. Combine the two parts to the array (the micro-seconds to the seconds).
4. Repeat steps 1,2 and 3 for the bottom of the page
5. Take the time taken at the end of the page from the time taken at the top of the page to determine the total loading time.
6. After rounding the microtime, return it to the browser.

At the top of your page, place:

$m_time = explode(” “,microtime());
$m_time = $m_time[0] + $m_time[1];
$starttime = $m_time;
?>

At the bottom of your page, place:

$round = 3;// The number of decimal places to round the micro time to.
$m_time = explode(” “,microtime());
$m_time = $m_time[0] + $m_time[1];
$endtime = $m_time;
$totaltime = ($endtime - $starttime);
echo “Page loading took:”. round($totaltime,$round) .” seconds”;
?>