WPDK  1.5.0
WordPress Development Kit
 All Data Structures Files Functions Variables Pages
wpdk-array.php
Go to the documentation of this file.
1 <?php
2 
16 class WPDKArray extends WPDKObject {
17 
25  public $__version = '1.0.3';
26 
39  public static function insertKeyValuePairs( $arr, $key, $val, $index = 0 )
40  {
41  $arrayEnd = array_splice( $arr, $index );
42  $arrayStart = array_splice( $arr, 0, $index );
43  return ( array_merge( $arrayStart, array( $key => $val ), $arrayEnd ) );
44  }
45 
63  public static function insert( &$arr, $new, $index = 0 )
64  {
65  /* Removes everything from offset */
66  $arrayEnd = array_splice( $arr, $index );
67  $arrayStart = array_splice( $arr, 0, $index );
68  return ( array_merge( $arrayStart, $new, $arrayEnd ) );
69  }
70 
83  public static function prepend( $array, &$dest )
84  {
85  $dest = self::insert( $dest, $array );
86  return $dest;
87  }
88 
101  public static function append( $array, &$dest )
102  {
103  $dest = self::insert( $dest, $array, count( $dest ) );
104  return $dest;
105  }
106 
117  public static function wrapArray( $array )
118  {
119  $result = array();
120  foreach ( $array as $element ) {
121  $result[] = array( $element );
122  }
123  return $result;
124  }
125 
138  public static function arrayMatch( $array_keys, $array_match )
139  {
140  $result = array();
141  foreach ( $array_match as $key => $value ) {
142  if ( in_array( $key, $array_keys ) ) {
143  $result[$key] = $value;
144  }
145  }
146  return $result;
147  }
148 
160  public static function arrayMatchWithValues( $array, $array_match )
161  {
162  return self::arrayMatch( array_values( $array ), $array_match );
163  }
164 
176  public static function arrayMatchWithKeys( $array, $array_match )
177  {
178  return self::arrayMatch( array_keys( $array ), $array_match );
179  }
180 
181  // -------------------------------------------------------------------------------------------------------------------
182  // Conversions
183  // -------------------------------------------------------------------------------------------------------------------
184 
194  public static function arrayToObject( $array )
195  {
196  // First we convert the array to a json string
197  $json = json_encode( $array );
198 
199  // The we convert the json string to a stdClass()
200  $object = json_decode( $json );
201 
202  return $object;
203  }
204 
205 
215  public static function objectToArray( $object )
216  {
217  // First we convert the object into a json string
218  $json = json_encode( $object );
219 
220  // Then we convert the json string to an array
221  $array = json_decode( $json, true );
222 
223  return $array;
224  }
225 
237  public static function stripKeys( $source, $keeplist )
238  {
239  $keys = array_keys( $keeplist );
240  foreach ( $source as $key => $value ) {
241  if ( !in_array( $key, $keys ) ) {
242  unset( $source[$key] );
243  }
244  }
245  return $source;
246  }
247 
260  public static function fit( $source, $defaults )
261  {
262  $source = self::stripKeys( $source, $defaults );
263  return array_merge( $defaults, $source );
264  }
265 
266  // -------------------------------------------------------------------------------------------------------------------
267  // Miscellanea
268  // -------------------------------------------------------------------------------------------------------------------
269 
281  public static function httpBuildQuery( $formdata, $numeric_prefix = null, $arg_separator = null )
282  {
283 
284  /* Get PHP version. */
285  if ( defined( 'PHP_MAJOR_VERSION' ) && PHP_MAJOR_VERSION >= 5 ) {
286  return http_build_query( $formdata, $numeric_prefix, $arg_separator );
287  }
288  else {
289  $version = absint( phpversion() );
290  if ( !empty( $version ) && $version >= 5 ) {
291  return http_build_query( $formdata, $numeric_prefix, $arg_separator );
292  }
293  }
294 
295  /* Emulate the behavior. */
296  $result = '';
297  $amp = '';
298  foreach ( $formdata as $key => $value ) {
299  $result .= sprintf( '%s%s=%s', $amp, $key, urlencode( $value ) );
300  $amp = $arg_separator;
301  }
302 
303  return $result;
304  }
305 
306  // -------------------------------------------------------------------------------------------------------------------
307  // Deprecated
308  // -------------------------------------------------------------------------------------------------------------------
309 
324  public static function arrayWithKey( $array_key, $array_extract )
325  {
326  _deprecated_function( __CLASS__ . '::' . __FUNCTION__, '1.4.8', 'arrayMatch()' );
327 
328  $keys = array_keys( $array_key );
329  $result = array();
330  foreach ( $array_extract as $key => $value ) {
331  if ( in_array( $key, $keys ) ) {
332  $result[$key] = $value;
333  }
334  }
335  return $result;
336  }
349  public static function arrayExtractByKey( $sourceArray, $arrayKeys )
350  {
351  _deprecated_function( __CLASS__ . '::' . __FUNCTION__, '1.4.8', 'arrayMatch()' );
352 
353  $keys = array_keys( $arrayKeys );
354  $result = array();
355  foreach ( $sourceArray as $key => $value ) {
356  if ( in_array( $key, $keys ) ) {
357  $result[$key] = $value;
358  }
359  }
360  return $result;
361  }
362 
363 }