Ubiquity  2.0.0
php rapid development framework
UArray.php
Go to the documentation of this file.
1 <?php
2 
4 
5 class UArray {
6 
7  public static function isAssociative($array) {
8  return (array_keys($array) !== range(0, count($array) - 1));
9  }
10 
11  public static function getValue($array, $key, $pos) {
12  if (array_key_exists($key, $array)) {
13  return $array[$key];
14  }
15  $values=array_values($array);
16  if ($pos < sizeof($values))
17  return $values[$pos];
18  }
19 
20  public static function getDefaultValue($array, $key, $default) {
21  if (array_key_exists($key, $array)) {
22  return $array[$key];
23  } else
24  return $default;
25  }
26 
27  public static function asPhpArray($array, $prefix="") {
28  $exts=array ();
29  $extsStr="";
30  if (self::isAssociative($array)) {
31  foreach ( $array as $k => $v ) {
32  $exts[]="\"" . $k . "\"=>" . self::parseValue($v, $prefix);
33  }
34  } else {
35  foreach ( $array as $v ) {
36  $exts[]=self::parseValue($v, $prefix);
37  }
38  }
39  if (\sizeof($exts) > 0 || $prefix !== "") {
40  $extsStr="(" . \implode(",", $exts) . ")";
41  }
42  return $prefix . $extsStr;
43  }
44 
45  public static function remove($array, $search) {
46  if (\is_array($search)) {
47  foreach ( $search as $val ) {
48  $array=self::removeOne($array, $val);
49  }
50  } else {
51  $array=self::removeOne($array, $search);
52  }
53  return array_values($array);
54  }
55 
56  public static function removeOne($array, $search) {
57  if (($key=array_search($search, $array)) !== false) {
58  unset($array[$key]);
59  }
60  return $array;
61  }
62 
63  public static function update(&$array, $search, $newValue) {
64  if (($key=array_search($search, $array)) !== false) {
65  $array[$key]=$newValue;
66  }
67  return $array;
68  }
69 
70  private static function parseValue($v, $prefix="") {
71  if (\is_bool($v) === true) {
72  $result=UString::getBooleanStr($v);
73  } elseif (\is_numeric($v)) {
74  $result=$v;
75  } elseif (\is_array($v)) {
76  $result=self::asPhpArray($v, $prefix);
77  } else {
78  $result="\"" . \str_replace('$', '\$', $v) . "\"";
79  }
80  return $result;
81  }
82 }
static removeOne($array, $search)
Definition: UArray.php:56
static getValue($array, $key, $pos)
Definition: UArray.php:11
static asPhpArray($array, $prefix="")
Definition: UArray.php:27
static getDefaultValue($array, $key, $default)
Definition: UArray.php:20
static parseValue($v, $prefix="")
Definition: UArray.php:70
static isAssociative($array)
Definition: UArray.php:7
static getBooleanStr($value)
Definition: UString.php:20
static update(&$array, $search, $newValue)
Definition: UArray.php:63