<?php
//Converts objects to arrays, arrays to objects, array strings to lower case, and array strings to upper case
//Works with Multi-Demensional arrays and objects
class arrayConverter {
//Converts objects to arrays
//pass an array to this function
function object_2_array($object){
$array = array();
foreach ($object as $key => $value) {
if (is_array($value) || is_object($value)) {
$array[$key] = $this->object_2_array($value);
} else {
$array[$key] = $value;
}
}
return $array;
}
//Converts array to object
//pass an array to this function
function array_2_object($array){
if(!is_array($array)){
return $array;
}
$object = new stdClass();
if(is_array($array) && !empty($array)){
foreach($array as $key => $value){
if(is_array($value)){
$object->$key = $this->array_2_object($value);
}else{
$object->$key = $value;
}
}
return $object;
}else{
return false;
}
}
//Convert array keys and values to lower case
//Works with objects and arrays
function array_to_lower($array){
if(is_object($array)){
$array = $this->object_2_array($array);
$convertBackToObject = true;
}else{
$convertBackToObject = false;
}
$newArray = array();
foreach($array as $key => $value){
$key = strtolower($key);
if(is_array($value)){
$newArray[$key] = $this->array_to_lower($value);
}else{
$newArray[$key] = strtolower($value);
}
}
if($convertBackToObject){
return $this->array_2_object($newArray);
}else{
return $newArray;
}
}
//Convert array keys to lower
//Works with objects and arrays
function array_keys_to_lower($array){
if(is_object($array)){
$array = $this->object_2_array($array);
$convertBackToObject = true;
}else{
$convertBackToObject = false;
}
$newArray = array();
foreach($array as $key => $value){
$key = strtolower($key);
if(is_array($value)){
$newArray[$key] = $this->array_keys_to_lower($value);
}else{
$newArray[$key] = $value;
}
$newArray[$key] = $value;
}
if($convertBackToObject){
return $this->array_2_object($newArray);
}else{
return $newArray;
}
}
//Convert array values to lower case
//Works with objects and arrays
function array_values_to_lower($array){
if(is_object($array)){
$array = $this->object_2_array($array);
$convertBackToObject = true;
}else{
$convertBackToObject = false;
}
$newArray = array();
foreach($array as $key => $value){
if(is_array($value)){
$newArray[$key] = $this->array_values_to_lower($value);
}else{
$newArray[$key] = strtolower($value);
}
}
if($convertBackToObject){
return $this->array_2_object($newArray);
}else{
return $newArray;
}
}
//Convert array keys and values to lower case
//Works with objects and arrays
function array_to_upper($array){
if(is_object($array)){
$array = $this->object_2_array($array);
$convertBackToObject = true;
}else{
$convertBackToObject = false;
}
$newArray = array();
foreach($array as $key => $value){
$key = strtoupper($key);
if(is_array($value)){
$newArray[$key] = $this->array_to_upper($value);
}else{
$newArray[$key] = strtoupper($value);
}
}
if($convertBackToObject){
return $this->array_2_object($newArray);
}else{
return $newArray;
}
}
//Convert array keys to lower
//Works with objects and arrays
function array_keys_to_upper($array){
if(is_object($array)){
$array = $this->object_2_array($array);
$convertBackToObject = true;
}else{
$convertBackToObject = false;
}
$newArray = array();
foreach($array as $key => $value){
$key = strtoupper($key);
if(is_array($value)){
$newArray[$key] = $this->array_keys_to_upper($value);
}else{
$newArray[$key] = $value;
}
$newArray[$key] = $value;
}
if($convertBackToObject){
return $this->array_2_object($newArray);
}else{
return $newArray;
}
}
//Convert array values to lower case
//Works with objects and arrays
function array_values_to_upper($array){
if(is_object($array)){
$array = $this->object_2_array($array);
$convertBackToObject = true;
}else{
$convertBackToObject = false;
}
$newArray = array();
foreach($array as $key => $value){
if(is_array($value)){
$newArray[$key] = $this->array_values_to_upper($value);
}else{
$newArray[$key] = strtoupper($value);
}
}
if($convertBackToObject){
return $this->array_2_object($newArray);
}else{
return $newArray;
}
}
}
?>
1 Response
Write a comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.