Skip to content

Instantly share code, notes, and snippets.

@samkeen
Created September 18, 2010 19:07
Show Gist options
  • Save samkeen/585948 to your computer and use it in GitHub Desktop.
Save samkeen/585948 to your computer and use it in GitHub Desktop.
PHP Array Util
<?php
/**
* array Util class
*/
class Arr {
/**
* $array Can be either an Array or the string output of
* print_r($array, true);
*/
public static function print_php($array) {
$array = is_array($array)?print_r($array,true):$array;
$array = preg_replace(
array(
'/Array\n/',
'/\[(.*)\] =>/',
'/\)\n/'
),
array(
"array\n",
'"${1}" =>',
"),\n"
),
$array);
// need regex skill++ , for now this will do
$array = preg_replace_callback(
'/=> (.*?)\n/',
// only replace if it is not the word 'array' or just digits
create_function(
'$matches',
'$match = isset($matches[1])?$matches[1]:null;
$qt = ctype_digit($match)||$match=="array"?"":"\"";
$c = $match=="array"?"":",";
return "=> {$qt}{$match}{$qt}{$c}\n";'
),
$array);
return $array;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment