Skip to content

Instantly share code, notes, and snippets.

Anver Sadutt anver

Block or report user

Report or block anver

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View Reference parent child matrix
function test_reference_tree_building() {
$source = [
1 => ['name' => 'Parent One', 'parent_id' => null], 2 => ['name' => 'Parent Two', 'parent_id' => null],
3 => ['name' => 'Child One', 'parent_id' => 1], 4 => ['name' => 'Child Two', 'parent_id' => 1],
5 => ['name' => 'Child Three', 'parent_id' => 2], 6 => ['name' => 'Child Four', 'parent_id' => 5],
7 => ['name' => 'Child Five', 'parent_id' => 2], 8 => ['name' => 'Child Six', 'parent_id' => 3],
9 => ['name' => 'Child Seven', 'parent_id' => 6]
];
$nested = [];
foreach ( $source as &$s ) {
View create matrix array from users
function get_matrix( $depths ) {
$all_users = $this->get_all_users();
$depths = [3, 3, 3];
$matrix = [];
for ( $level = 0; $level < count( $depths ); $level++ ) {
$total_members = pow( $depths[$level], $level + 1 );
$members = json_decode( json_encode( array_splice( $all_users, 0, $total_members ), JSON_FORCE_OBJECT ) );
$members_arr = json_decode( json_encode( $members ), TRUE );
$iter = new RecursiveIteratorIterator( new RecursiveArrayIterator( $members ), RecursiveIteratorIterator::SELF_FIRST );
View Converting Array to Objects
The below code converted the outer most array to object and not the nested arrays.
$object = json_decode(json_encode($array), false);
but this will force all sub elements to objects
$object = json_decode(json_encode($array, JSON_FORCE_OBJECT), false);
I'm pretty sure the solution would be similar when converting from object to array.
View RecursiveIteratorIterator offsetSet Example
function test_set_values() {
$data = [
'test' => 'value',
'level_one' => [
'level_two' => [
'level_three' => [
'replace_this_array' => [
'key_one' => 'testing',
'key_two' => 'value',
'special_key' => 'replacement_value',
View RecursiveIteratorIterator
$a = array();
// Values inside of ArrayObject instances will be changed correctly, values
// inside of plain arrays won't
$a[] = array(new ArrayObject( range( 100, 200, 100 ) ),
new ArrayObject( range( 200, 100, -100 ) ),
range( 100, 200, 100 ));
$a[] = new ArrayObject( range( 225, 75, -75 ) );
// The array has to be
View set elongated array keys
function test_set_elongated_keys() {
$data = [
'test' => 'value',
'level_one' => [
'level_two' => [
'level_three' => [
'replace_this_array' => [
'key_one' => 'testing',
'key_two' => 'value',
'special_key' => 'replacement_value',
View functions.php
<?php
/**
* Recursively get taxonomy hierarchy
*
* @source http://www.daggerhart.com/wordpress-get-taxonomy-hierarchy-including-children/
* @param string $taxonomy
* @param int $parent - parent term id
*
* @return array
@anver
anver / Documentation.md
Created Sep 13, 2017 — forked from KartikTalwar/Documentation.md
Rsync over SSH - (40MB/s over 1GB NICs)
View Documentation.md

The fastest remote directory rsync over ssh archival I can muster (40MB/s over 1gb NICs)

This creates an archive that does the following:

rsync (Everyone seems to like -z, but it is much slower for me)

  • a: archive mode - rescursive, preserves owner, preserves permissions, preserves modification times, preserves group, copies symlinks as symlinks, preserves device files.
  • H: preserves hard-links
  • A: preserves ACLs
@anver
anver / scp-git-diff.sh
Created Jun 4, 2017 — forked from tanakahisateru/scp-git-diff.sh
Send changed files under Git via SCP (if SSH password auth)
View scp-git-diff.sh
#!/bin/sh
if [ $# -ne 1 ]; then
echo "$0 <git-commit>" 1>&2
exit 1
fi
git diff --name-status $1
read -p "Press any key to execute..."
@anver
anver / parser.php
Last active Sep 20, 2015 — forked from martinsik/parser.php
Simple sport results parser in PHP using XPath. For more information visit http://martinsikora.com/parsing-html-pages-using-xpath
View parser.php
<?php
$curl = curl_init('http://www.livescore.com/soccer/england/');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.10 (KHTML, like Gecko) Chrome/8.0.552.224 Safari/534.10');
$html = curl_exec($curl);
curl_close($curl);
if (!$html) {
You can’t perform that action at this time.