Skip to content

Instantly share code, notes, and snippets.

View anver's full-sized avatar
🏠
Working from home

Anver Sadutt anver

🏠
Working from home
View GitHub Profile
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 ) {
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 );
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.
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',
$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
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',
@anver
anver / functions.php
Created June 13, 2018 18:34 — forked from tripflex/functions.php
WordPress Recursively get taxonomy hierarchy (courtesy of http://www.daggerhart.com/wordpress-get-taxonomy-hierarchy-including-children/ )
<?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 September 13, 2017 07:48 — forked from KartikTalwar/Documentation.md
Rsync over SSH - (40MB/s over 1GB NICs)

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 June 4, 2017 20:08 — forked from tanakahisateru/scp-git-diff.sh
Send changed files under Git via SCP (if SSH password auth)
#!/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 September 20, 2015 10:45 — 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
<?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) {