Skip to content

Instantly share code, notes, and snippets.

@wajdijurry
Last active July 6, 2021 15:21
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wajdijurry/a00752123134342ccc9d13a480c9f013 to your computer and use it in GitHub Desktop.
Save wajdijurry/a00752123134342ccc9d13a480c9f013 to your computer and use it in GitHub Desktop.
Adding item to array or change existent item position after shifting elements up or down depending on available positions
<?php
/**
* Author:Wajdi Jurry <jurrywajdi@yahoo.com> (https://github.com/wajdijurry)
* Gist: https://gist.github.com/wajdijurry/a00752123134342ccc9d13a480c9f013
*/
class ArrayManipulation
{
private $array;
public function __construct(array $array)
{
$this->array = $array;
}
/**
* Add/Change item position in array
* Shifting elements up/down depending on available positions
* Priority for shifting down
* @param int $position
* @param string $itemId
* @return array
*/
public function addItemChangePosition(int $position, string $itemId)
{
$max = 5; // Max number of elements in array
if ($position >= $max || $position < 0) {
exit('Wrong Position');
}
// Remove item if exists
if (($key = array_search($itemId, $this->array)) !== false) {
unset($this->array[$key]);
}
$newArr = [];
if (array_key_exists($position, $this->array)) {
/**
* Determing shifting up or down
* Shifting up: 1, Shifting Down: -1
*/
$operator = (array_sum(array_keys($this->array)) >= array_sum(range($position, $max-1))) ? 1 : -1;
//Iterate over array, starting from $position till nonexistent element
for ($i = $position; $i >= $position * (-$operator); $i = $i - $operator) {
if (!array_key_exists($i, $this->array)) {
// Exit loop if element does not exist
break;
}
// Add items-to be shifted to $newArr
$newArr[$i - $operator] = $this->array[$i];
// Remove from original array (avoiding duplicate)
unset($this->array[$i]);
}
// Merging shifted elements with non-affected elements, keys preserved
$this->array = $newArr + array_diff($this->array, $newArr);
}
// Add new item to array
$this->array[$position] = $itemId;
// Sort array
ksort($this->array, SORT_NUMERIC);
return $this->array;
}
}
// Example 1:
$items = [
0 => 'item1',
1 => 'item2'
];
print_r($items = (new ArrayManipulation($items))->addItemChangePosition(1, 'new item')); // $items = [0 => 'item1', 1 => 'new item', 2 => 'item2']
// Example 2:
$items = [
0 => 'item1',
3 => 'item2'
];
print_r($items = (new ArrayManipulation($items))->addItemChangePosition(3, 'new item')); // $items = [0 => 'item1', 3 => 'new item', 4 => 'item2']
// Example 3:
$items = [
1 => 'item1',
2 => 'item2',
3 => 'item3',
4 => 'item4'
];
print_r($items = (new ArrayManipulation($items))->addItemChangePosition(4, 'new item')); // $items = [0 => 'item1', 1 => 'item2', 2 => 'item3', 3 => 'item4', 4 => 'new item']
// Example 3:
$items = [
1 => 'item1',
2 => 'item2',
3 => 'item3',
4 => 'item4'
];
print_r($items = (new ArrayManipulation($items, ))->addItemChangePosition(2, 'item4')); // $items = [1 => 'item1', 2 => 'item4', 3 => 'item2', 4 => 'item3']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment