Skip to content

Instantly share code, notes, and snippets.

@blar
blar / gist:9002307
Created February 14, 2014 14:50
array_path
<?php
function array_path($array, $path) {
$context = $array;
$parts = explode('.', $path);
while($key = array_shift($parts)) {
if(!is_array($context)) {
return NULL;
}
if(!array_key_exists($key, $context)) {
<?php
class foo {
public static function bar() {
return 'foobar';
}
}
<?php
function array_partition($array, $callback, $preserveKeys = false) {
$partitions = array();
foreach($array as $key => $value) {
$partitionName = $callback($value, $key);
if(!array_key_exists($partitionName, $partitions)) {
$partitions[$partitionName] = array();
}
if($preserveKeys) {
@blar
blar / gist:8982098
Created February 13, 2014 19:29
array_partition
<?php
function array_partition($array, $callback, $preserveKeys = false) {
$partitions = array();
foreach($array as $key => $value) {
$partitionName = $callback($value, $key);
if(!array_key_exists($partitionName, $partitions)) {
$partitions[$partitionName] = array();
}
if($preserveKeys) {
<?php
$foo = new DateTime('2014W08');
$bar = new DateTime('tuesday this week');
$diff = $foo->diff($bar);
var_dump($diff->format('%a'));
string(1) "6"
<?php
$arguments = xmlrpc_decode_request($xml, $method);
switch($method) {
case 'get_topic':
$_GET['forum_id'] = $arguments[0];
$_GET['start_num'] = $arguments[1];
$_GET['last_num'] = $arguments[2];
$_GET['mode'] = $arguments[3];
@blar
blar / gist:8194547
Created December 31, 2013 09:37
Refactoring
<?php
/**
* Erste einfache Version.
*/
function naturalDate($timestamp) {
$diff = time() - $timestamp;
if($diff < 86400) {
return 'Heute';
@blar
blar / gist:7891941
Created December 10, 2013 14:56
array_key_rename
<?php
function array_key_rename($array, $old, $new) {
$array[$new] = $array[$old];
unset($array[$old]);
return $array;
}
@blar
blar / gist:7890893
Last active December 30, 2015 21:59
<style>
ul {
display: -webkit-flex;
border-spacing: 4px;
padding: 0;
-webkit-flex-wrap: wrap;
}
li {
width: 12rem;
@blar
blar / gist:6796150
Created October 2, 2013 16:08
INTL MessageFormatter mit benannten Parametern
<?php
$formatter = new MessageFormatter('de', '{comments, plural, =0 {Keine Kommentare} =1 {# Kommentar} other {# Kommentare}}');
for($comments = 0; $comments < 10; $comments++) {
var_dump($formatter->format(array(
'comments' => $comments
)));
}