Skip to content

Instantly share code, notes, and snippets.

View mtdowling's full-sized avatar

Michael Dowling mtdowling

View GitHub Profile
@mtdowling
mtdowling / gist:3877616
Last active September 30, 2021 06:34
Recursive XML to array function
<?php
function xml2array($xml)
{
$arr = array();
foreach ($xml->getNamespaces() + array(null) as $prefix => $namespace) {
foreach ($xml->attributes($namespace) as $key => $value) {
// Add prefixes to prefixed attributes
if (is_string($prefix)) {
@mtdowling
mtdowling / ModuleGraph.java
Created April 10, 2019 16:21
Module Graph example
package com.example;
import java.io.File;
import java.lang.module.ModuleDescriptor;
import java.lang.module.ModuleFinder;
import java.lang.module.ModuleReference;
import java.net.URI;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayDeque;
@mtdowling
mtdowling / gist:4516616
Created January 12, 2013 07:42
Various methods of calculating Fibonacci numbers in PHP
<?php
/**
* Various methods of computing Fibonacci numbers in PHP
*/
class Fibonacci
{
/**
* @var array Memoization cache
* @see Fibonacci::memoized
@mtdowling
mtdowling / gist:4516558
Last active March 7, 2018 00:56
Fibonacci in PHP using anonymous functions and memoization
<?php
$fibMemo = call_user_func(function () {
$memo = array(0 => 0, 1 => 1);
$fib = function ($n) use (&$memo, &$fib) {
if (!isset($memo[$n])) {
$memo[$n] = $fib($n - 1) + $fib($n - 2);
}
return $memo[$n];
};
@mtdowling
mtdowling / MovementSystem.lua
Created February 8, 2017 06:21
A slice of some of my collision rounding code
local ecs = require "ecs"
local collision = require "collision"
local components = require "components"
local utils = require "utils"
local clamp = lume.clamp
--- Updates entity physics.
local MovementSystem = ecs.createSystem("Movement", "pos", "motion")
@mtdowling
mtdowling / can_json_decode.php
Created March 29, 2017 21:27
Check if a value can be json_decoded
<?php
function try_json_encode($data)
{
json_encode($data);
return (json_last_error() == JSON_ERROR_NONE);
}
function json_encode_type_check($data)
{
@mtdowling
mtdowling / react-guzzle.php
Created September 9, 2015 22:37
React, Guzzle, and AWS SDK for PHP integration
<?php
require 'vendor/autoload.php';
use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\Response;
use React\EventLoop\Factory;
use WyriHaximus\React\GuzzlePsr7\HttpClientAdapter;
$loop = Factory::create();
@mtdowling
mtdowling / simple.js
Created July 25, 2013 17:13
Simple node.js server
var http = require("http");
http.createServer(function(req, res) {
req.addListener("data", function(chunk) {
// Read data chunks
});
// Called when the request completes
req.addListener("end", function() {
res.writeHead(200, "OK", { "Content-Length": 10 });
@mtdowling
mtdowling / readfuction_perf.php
Created July 23, 2013 00:52
Simple script to reproduce CURLOPT_READFUNCTION performance issue
<?php
$m = curl_multi_init();
function sendMulti($ch)
{
global $m;
curl_multi_add_handle($m, $ch);
$active = false;
@mtdowling
mtdowling / gist:3877687
Created October 12, 2012 06:54
XML to array using json_encode
<?php
// Note that this will not maintain XML attributes
function jsonXml2Array(SimpleXMLElement $xml)
{
return json_decode(json_encode($xml), true);
}
function jsonXml2ArrayWithNamespacedAttributes($xml)
{