function pleac_Extracting_Unique_Elements_from_a_List() { // PHP offers the 'array_unique' function to perform this task. It works with both keyed, // and numerically-indexed arrays; keys / indexes are preserved; use of 'array_values' // is recommended to reindex numerically-indexed arrays since there will likely be missing // indexes // Remove duplicate values $unique = array_unique($array); // Remove duplicates, and reindex [for numerically-indexed arrays only] $unique = array_values(array_unique($array)); // or use: $unique = array_keys(array_flip($array)); // ---------------------------- // Selected Perl 'seen' examples foreach ($list as $item) { if (!isset($seen[$item])) { $seen[$item] = TRUE; $unique[] = $item; } } // ------------ foreach ($list as $item) { $seen[$item] || ++$seen[$item] && ($unique[] = $item); } // ------------ function some_func($item) { // Do something with '$item' } foreach ($list as $item) { $seen[$item] || ++$seen[$item] && some_func($item); } // ---------------------------- foreach (array_slice(preg_split('/\\n/', `who`), 0, -1) as $user_entry) { $user = preg_split('/\\s/', $user_entry); $ucnt[$user[0]]++; } ksort($ucnt); echo "users logged in:\n"; foreach ($ucnt as $user => $cnt) { echo "\t{$user} => {$cnt}\n"; } }
function pleac_Returning_More_Than_One_Array_or_Hash() { // Multiple return values are possible via packing a set of values within a // numerically-indexed array and using 'list' to extract them function some_func() { return array(array(1, 2, 3), array('a' => 1, 'b' => 2)); } // ------------ list($arr, $hash) = some_func(); // ---------------------------- function some_func(&$arr, &$hash) { return array($arr, $hash); } // ------------ $arrin = array(1, 2, 3); $hashin = array('a' => 1, 'b' => 2); list($arr, $hash) = some_func($arrin, $hashin); }
PtcDebug::bufferLog($array, 'testing an array', 'new category'); /* LOGGING AN OBJECT */ PtcDebug::bufferLog((object) $array, 'testing an object'); /* THROWING A NOTICE */ trigger_error('some notice', E_USER_NOTICE); /* THROWING A WARNING */ trigger_error('some warning', E_USER_WARNING); /* THROWING AN ERROR */ trigger_error('some error', E_USER_ERROR); // continue execution with the options "die_on_error" set to false /* TESTING AN ERROR WITHIN A FUNCTION */ function some_func() { fopen(); } echo some_func(); // will throw an error /* LOGGING SQL QUERIES AND TIMING EXECUTION */ $sql = 'select from where something'; // some sql query, will be used as reference PtcDebug::bufferSql('', $sql); // leaving the first parameter empty, can be added later with the query result $sql_result = array('key' => 'value', 'key1' => 'value1'); // this should be the sql result of the sql query PtcDebug::stopTimer($sql); // time execution, the query is used as reference PtcDebug::addToBuffer($sql, $sql_result); // attaching the result to the message based on the reference /* WATCHING A VARIABLE */ declare (ticks=1) { $var = 'some test';