Skip to content

Instantly share code, notes, and snippets.

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

Andreas Bee beephotography

🏠
Working from home
View GitHub Profile
@beephotography
beephotography / flatArrayToTree.php
Last active November 13, 2019 07:36
Converts a flat array to a tree structure with references
<?php
/**
* @param array $navigation
* @return array
*/
private function flatArrayToTree(array $navigation): array
{
$navigationTree = [];
@beephotography
beephotography / swapArrayElements.js
Last active October 27, 2019 13:09
Swaps to elements of an array without mutating the original array. Returns the swapped array.
const swapArrayElements = (arr, index1, index2) => arr.map((val, idx) => {
if (idx === index1) return arr[index2];
if (idx === index2) return arr[index1];
return val;
});
const foo = [1, 2, 3, 4, 5];
const swapped = swapArrayElements(foo, 1, 3);
console.log(foo); //=> [1, 2, 3, 4, 5]
console.log(swapped); //=> [ 1, 4, 3, 2, 5 ]