GlisWeb framework
_array.tools.php
Vai alla documentazione di questo file.
1 <?php
2 
14  // costanti per la gestione dei files csv
15  define( 'ARRAY_SORT_ASC' , 'ASC' );
16  define( 'ARRAY_SORT_DSC' , 'DSC' );
17  define( 'ARRAY_SEPARATOR' , '|' );
18  define( 'CHECK_BY_KEY' , 'CBK' );
19  define( 'CHECK_BY_VALUE' , 'CBV' );
20 
26  function string2array( &$s, $c = ARRAY_SEPARATOR ) {
27  $s = explode( $c, $s );
28  }
29 
35  function array2string( &$a, $c = ARRAY_SEPARATOR ) {
36  $a = implode( $c, $a );
37  }
38 
44  function rksort( &$array ) {
45  if( is_array( $array ) ) {
46  ksort( $array );
47  array_walk( $array, 'rksort' );
48  }
49  }
50 
56  function trimArray( &$ar, $limit = 0 ) {
57 /*
58  $i = count( $ar ) - 1;
59 
60  while( $i >= 0 && empty( $ar[ $i ] ) && $i >= $limit ) {
61  array_pop( $ar );
62  $i--;
63  }
64 */
65  $ar = array_map( 'trim', $ar );
66  $ar = array_filter( $ar );
67 
68  return $ar;
69 
70  }
71 
77  function removeFromArray( &$a, $e ) {
78 
79  if( ! is_array( $e ) ) { $e = array( $e ); }
80 
81  $a = array_diff( $a, $e );
82 
83  }
84 
90  function arrayLowercase( &$a ) {
91 
92  $a = array_map( 'strtolower', $a );
93 
94  }
95 
101  if( ! function_exists( 'array_key_first' ) ) {
102 
103  function array_key_first( $a ) {
104  reset( $a );
105  return key( $a );
106  }
107 
108  }
109 
115  if( ! function_exists( 'array_column' ) ) {
116 
117  function array_column( $a, $k ) {
118 
119  $r = array();
120 
121  if( is_array( $a ) ) {
122  foreach( $a as $v ) {
123 # NOTA questo controllo è stato commentato per compatibilità con array_filter() di PHP
124 # if( isset( $v[ $k ] ) ) {
125  $r[] = $v[ $k ];
126 # }
127  }
128  }
129 
130  return $r;
131 
132  }
133 
134  }
135 
141  function arraySortBy( $field, &$array, $direction = ARRAY_SORT_ASC ) {
142 
143  usort( $array,
144  create_function('$a, $b', '
145  $a = $a["' . $field . '"];
146  $b = $b["' . $field . '"];
147  if ( $a == $b ) return 0;
148  return ( $a ' . ( $direction == ARRAY_SORT_DSC ? '>' : '<' ) .' $b ) ? -1 : 1;
149  ')
150  );
151 
152  return true;
153 
154  }
155 
162  function arrayFilterBy( $field, $match, $array ) {
163 
164  return false;
165 
166  }
167 
173  function arrayKeyValuesImplode( $array, $tk1, $tk2, $empty = false ) {
174 
175  $t = array();
176 
177  foreach( $array as $k => $v ) {
178  if( ! empty( $v ) || $empty === true ) {
179  $t[] = $k . $tk1 . $v;
180  }
181  }
182 
183  return implode( $tk2, $t );
184 
185  }
186 
192  function arrayInsertAssoc( $ref, &$data, $array ) {
193 
194  $r = array();
195 
196  foreach( $array as $k => $v ) {
197 
198  $r[ $k ] = $v;
199 
200  if( $k == $ref ) {
201  foreach( $data as $y => $j ) {
202  $r[ $y ] = $j;
203  }
204  }
205 
206  }
207 
208  }
209 
215  function arrayInsertSeq( $ref, &$target, $add ) {
216 
217  // echo '--' . $ref . PHP_EOL;
218  // print_r( $target );
219  // echo array_search( $ref, $target ) . PHP_EOL;
220  // print_r( $add );
221 
222  array_splice( $target, ( array_search( $ref, $target ) + 1 ), 0, $add );
223 
224  // print_r( $target );
225 
226  }
227 
228  function addStr2arrayElements( $a, $p = NULL, $s = NULL ) {
229 
230  return array_map( function( $v ) use ( $p, $s ) {
231  return $p . $v . $s;
232  }, $a );
233 
234  }
235 
236 ?>
array2string(&$a, $c=ARRAY_SEPARATOR)
const ARRAY_SEPARATOR
string2array(&$s, $c=ARRAY_SEPARATOR)
addStr2arrayElements( $a, $p=NULL, $s=NULL)
$p['ricerca']
rksort(&$array)
removeFromArray(&$a, $e)
$a
Definition: _slack.php:21
const ARRAY_SORT_DSC
trimArray(&$ar, $limit=0)
arrayLowercase(&$a)
$r
Definition: _osm.php:25
$e
Definition: _slack.php:121
arrayInsertSeq( $ref, &$target, $add)
arrayInsertAssoc( $ref, &$data, $array)
const ARRAY_SORT_ASC
arrayFilterBy( $field, $match, $array)
arrayKeyValuesImplode( $array, $tk1, $tk2, $empty=false)
if(! function_exists( 'array_key_first')) if(! function_exists( 'array_column')) arraySortBy( $field, &$array, $direction=ARRAY_SORT_ASC)