Ubiquity  2.0.0
php rapid development framework
UArray.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Ubiquity\utils;
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  private static function parseValue($v, $prefix="") {
64  if (\is_bool($v) === true) {
65  $result=UString::getBooleanStr($v);
66  } elseif (\is_numeric($v)) {
67  $result=$v;
68  } elseif (\is_array($v)) {
69  $result=self::asPhpArray($v, $prefix);
70  } else {
71  $result="\"" . \str_replace('$', '\$', $v) . "\"";
72  }
73  return $result;
74  }
75 }
static asPhpArray($array, $prefix="")
Definition: UArray.php:27
static removeOne($array, $search)
Definition: UArray.php:56
static getBooleanStr($value)
Definition: UString.php:20
static isAssociative($array)
Definition: UArray.php:7
static getDefaultValue($array, $key, $default)
Definition: UArray.php:20
static getValue($array, $key, $pos)
Definition: UArray.php:11
static parseValue($v, $prefix="")
Definition: UArray.php:63