Ubiquity  2.0.3
php rapid development framework
UArray.php
Go to the documentation of this file.
1 <?php
2 
4 
10 class UArray {
11 
17  public static function isAssociative($array) {
18  return (array_keys($array) !== range(0, count($array) - 1));
19  }
20 
27  public static function extractKeys($array,$keys){
28  $result=[];
29  foreach ($keys as $key){
30  if(isset($array[$key])){
31  $result[$key]=$array[$key];
32  }
33  }
34  return $result;
35  }
36 
37  public static function getValue($array, $key, $pos) {
38  if (array_key_exists($key, $array)) {
39  return $array[$key];
40  }
41  $values=array_values($array);
42  if ($pos < sizeof($values))
43  return $values[$pos];
44  }
45 
46  public static function getDefaultValue($array, $key, $default) {
47  if (array_key_exists($key, $array)) {
48  return $array[$key];
49  } else
50  return $default;
51  }
52 
53  public static function asPhpArray($array, $prefix="",$depth=1,$format=false) {
54  $exts=array ();
55  $extsStr="";$tab="";$nl="";
56  if($format){
57  $tab=str_repeat("\t",$depth);
58  $nl=PHP_EOL;
59  }
60  if (self::isAssociative($array)) {
61  foreach ( $array as $k => $v ) {
62  $exts[]="\"" . UString::doubleBackSlashes($k) . "\"=>" . self::parseValue($v, 'array',$depth+1,$format);
63  }
64  } else {
65  foreach ( $array as $v ) {
66  $exts[]=self::parseValue($v, 'array',$depth+1,$format);
67  }
68  }
69  if (\sizeof($exts) > 0 || $prefix !== "") {
70  $extsStr="(" . \implode(",".$nl.$tab, $exts).")";
71  if(\sizeof($exts)>0){
72  $extsStr="(" .$nl.$tab. \implode(",".$nl.$tab, $exts).$nl.$tab.")";
73  }
74  }
75  return $prefix . $extsStr;
76  }
77 
78  public static function asJSON($array){
79  return json_encode($array, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP | JSON_UNESCAPED_UNICODE);
80  }
81 
82  public static function remove($array, $search) {
83  if (\is_array($search)) {
84  foreach ( $search as $val ) {
85  $array=self::removeOne($array, $val);
86  }
87  } else {
88  $array=self::removeOne($array, $search);
89  }
90  return array_values($array);
91  }
92 
99  public static function removeByKey($array,$key){
100  if(isset($array[$key])){
101  unset($array[$key]);
102  }
103  return $array;
104  }
105 
106  public static function removeOne($array, $search) {
107  if (($key=array_search($search, $array)) !== false) {
108  unset($array[$key]);
109  }
110  return $array;
111  }
112 
113  public static function update(&$array, $search, $newValue) {
114  if (($key=array_search($search, $array)) !== false) {
115  $array[$key]=$newValue;
116  }
117  return $array;
118  }
119 
120  public static function doubleBackSlashes(&$array){
121  return array_walk($array, function($value){$value=UString::doubleBackSlashes($value);});
122  }
123 
124  private static function parseValue($v, $prefix="",$depth=1,$format=false) {
125  if (UString::isBooleanStr($v)) {
126  $result=UString::getBooleanStr($v);
127  } elseif (\is_numeric($v)) {
128  $result=$v;
129  } elseif (\is_array($v)) {
130  $result=self::asPhpArray($v, $prefix,$depth+1,$format);
131  }elseif(UString::startswith(trim($v), "function") || UString::startswith(trim($v), "array(")){
132  $result=$v;
133  } else {
134  $result="\"" . \str_replace('$', '\$', $v) . "\"";
135  $result=UString::doubleBackSlashes($result);
136  }
137  return $result;
138  }
139 }
static removeOne($array, $search)
Definition: UArray.php:106
Array utilities.
Definition: UArray.php:10
static getValue($array, $key, $pos)
Definition: UArray.php:37
static extractKeys($array, $keys)
Returns a new array with the keys $keys.
Definition: UArray.php:27
static doubleBackSlashes($value)
Definition: UString.php:93
static doubleBackSlashes(&$array)
Definition: UArray.php:120
static getDefaultValue($array, $key, $default)
Definition: UArray.php:46
static parseValue($v, $prefix="", $depth=1, $format=false)
Definition: UArray.php:124
static isAssociative($array)
Tests if array is associative.
Definition: UArray.php:17
static getBooleanStr($value)
Definition: UString.php:20
static update(&$array, $search, $newValue)
Definition: UArray.php:113
static removeByKey($array, $key)
Removes from array by key.
Definition: UArray.php:99
static asJSON($array)
Definition: UArray.php:78
static isBooleanStr($value)
Definition: UString.php:47
static startswith($hay, $needle)
Definition: UString.php:12
static asPhpArray($array, $prefix="", $depth=1, $format=false)
Definition: UArray.php:53