| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package FirePlugins Framework |
| 4 |
* @version 1.1.102 |
| 5 |
* |
| 6 |
* @author FirePlugins <info@fireplugins.com> |
| 7 |
* @link https://www.fireplugins.com |
| 8 |
* @copyright Copyright © 2024 FirePlugins All Rights Reserved |
| 9 |
* @license GNU GPLv3 <http://www.gnu.org/licenses/gpl.html> or later |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace FPFramework\Helpers; |
| 13 |
|
| 14 |
if (!defined('ABSPATH')) |
| 15 |
{ |
| 16 |
exit; // Exit if accessed directly. |
| 17 |
} |
| 18 |
|
| 19 |
class ArrayHelper |
| 20 |
{ |
| 21 |
/** |
| 22 |
* Fix arrays, remove duplicate items, null items and whitespace around item values. |
| 23 |
* |
| 24 |
* @param array $subject |
| 25 |
* |
| 26 |
* @return array The new cleaned array |
| 27 |
*/ |
| 28 |
public static function cleanArray($subject) |
| 29 |
{ |
| 30 |
if (!is_array($subject)) |
| 31 |
{ |
| 32 |
return $subject; |
| 33 |
} |
| 34 |
|
| 35 |
$subject = array_unique($subject); |
| 36 |
$subject = array_map('trim', $subject); |
| 37 |
|
| 38 |
// Remove empty items. We use a custom callback here because the default behavior of array_filter removes 0 values as well. |
| 39 |
$subject = array_filter($subject, function($value) |
| 40 |
{ |
| 41 |
return ($value !== null && $value !== false && $value !== ''); |
| 42 |
}); |
| 43 |
|
| 44 |
return $subject; |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* Merges 2 arrays |
| 49 |
* |
| 50 |
* @param array $arr1 |
| 51 |
* @param array $arr2 |
| 52 |
* |
| 53 |
* @return return |
| 54 |
*/ |
| 55 |
public static function arrayMerge($arr1, $arr2) |
| 56 |
{ |
| 57 |
$new_arr = $arr1; |
| 58 |
|
| 59 |
foreach ($arr2 as $key => $value) |
| 60 |
{ |
| 61 |
if (isset($new_arr[$key]) && is_array($new_arr[$key]) && is_array($value)) |
| 62 |
{ |
| 63 |
$tmp = array_unique(array_merge($new_arr[$key], $value)); |
| 64 |
$new_arr[$key] = $tmp; |
| 65 |
} |
| 66 |
else |
| 67 |
{ |
| 68 |
$new_arr[$key] = $value; |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
return $new_arr; |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Method to determine if an array is an associative array. |
| 77 |
* |
| 78 |
* @param array $array An array to test. |
| 79 |
* |
| 80 |
* @return boolean |
| 81 |
*/ |
| 82 |
public static function isAssociative($array) |
| 83 |
{ |
| 84 |
if (\is_array($array)) |
| 85 |
{ |
| 86 |
foreach (array_keys($array) as $k => $v) |
| 87 |
{ |
| 88 |
if ($k !== $v) |
| 89 |
{ |
| 90 |
return true; |
| 91 |
} |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
return false; |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Inserts value after given key. |
| 100 |
* |
| 101 |
* @param array $input The input where we will search and add the new $element |
| 102 |
* @param mixed $index The index that we use to find the key and insert after |
| 103 |
* @param mixed $newKey The new index that will be used for this key, value pair |
| 104 |
* @param mixed $newValue The new value of the key, value pair |
| 105 |
* |
| 106 |
* @return array |
| 107 |
*/ |
| 108 |
public static function insertAfter($input, $index, $newKey, $newValue) |
| 109 |
{ |
| 110 |
if (!array_key_exists($index, $input)) |
| 111 |
{ |
| 112 |
throw new \Exception('Index not found'); |
| 113 |
} |
| 114 |
|
| 115 |
$tmpArray = []; |
| 116 |
|
| 117 |
foreach ($input as $key => $value) |
| 118 |
{ |
| 119 |
$tmpArray[$key] = $value; |
| 120 |
|
| 121 |
if ($key === $index) |
| 122 |
{ |
| 123 |
$tmpArray[$newKey] = $newValue; |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
return $tmpArray; |
| 128 |
} |
| 129 |
} |