PHP: Convert Array to Object and vice-versa

Here we will give you code snippets or tips related to PHP/MySQL/XHTML/CSS/JavaScript/AJAX development
Forum rules
Be civilized... # Seja civilizado... # Être civilisé...

PHP: Convert Array to Object and vice-versa

Postby weblivehelp on Thu May 01, 2008 5:40 pm

Hi there,

Welcome to the first WebLive Help Tutorial.

Today we'll give out two very useful PHP functions:
  • Convert/Parse Array to Object
  • Convert/Parse Object to Array

Code: Select all
<?php
function parseArrayToObject($array) {
   $object = new stdClass();
   if (is_array($array) && count($array) > 0) {
      foreach ($array as $name=>$value) {
         $name = strtolower(trim($name));
         if (!empty($name)) {
            $object->$name = $value;
         }
      }
   }
   return $object;
}

function parseObjectToArray($object) {
   $array = array();
   if (is_object($object)) {
      $array = get_object_vars($object);
   }
   return $array;
}
?>


Usage:

Convert Array $a to Object $o

Code: Select all
<?php
$a = array (
   'index_0' => 'value_0',
   'index_1' => 'value_1'
);

$o = parseArrayToObject($a);

//-- Now you can use $o like this:
echo $o->index_0;//-- Will print 'value_0'
?>


Convert Object $o to Array $a

Code: Select all
<?php
$o = new stdClass();
$o->index_0 = 'value_0';
$o->index_1 = 'value_1';

$a = parseObjectToArray($o);

//-- Now you can use $a like this:
echo $a['index_0'];//-- Will print 'value_0'
?>


Use it well! ;)
weblivehelp
Site Admin
 
Posts: 6
Joined: Thu May 01, 2008 3:23 pm

Re: PHP: Convert Array to Object and vice-versa

Postby Guest on Mon Jun 30, 2008 8:45 am

Thanks! Very useful! Keep it up ;)
Guest
 

Re: PHP: Convert Array to Object and vice-versa

Postby phpguy on Wed Jul 23, 2008 4:24 pm

Just what I was looking for!!! Thanks!
phpguy
 

Re: PHP: Convert Array to Object and vice-versa

Postby marcel on Sat Jul 26, 2008 6:33 pm

a recursive way to convert multidimensional arrays and objects

Code: Select all
function array2object($data) {
   if(!is_array($data)) return $data;
   
   $object = new stdClass();
   if (is_array($data) && count($data) > 0) {
      foreach ($data as $name=>$value) {
         $name = strtolower(trim($name));
         if (!empty($name)) {
            $object->$name = array2object($value);
         }
      }
   }
   return $object;
}
function object2array($data){
   if(!is_object($data) && !is_array($data)) return $data;

   if(is_object($data)) $data = get_object_vars($data);

   return array_map('object2array', $data);
}
marcel
 

Re: PHP: Convert Array to Object and vice-versa

Postby weblivehelp on Tue Jul 29, 2008 6:33 pm

Thanks marcel for the tip ;)
weblivehelp
Site Admin
 
Posts: 6
Joined: Thu May 01, 2008 3:23 pm

Re: PHP: Convert Array to Object and vice-versa

Postby dam on Mon Aug 25, 2008 12:08 am

marcel wrote:
$name = strtolower(trim($name));

great function indeed, but can't understand why u use strtolower

http://dventurin.wordpress.com
dam
 

Re: PHP: Convert Array to Object and vice-versa

Postby weblivehelp on Mon Aug 25, 2008 8:59 am

dam wrote:
marcel wrote:
$name = strtolower(trim($name));

great function indeed, but can't understand why u use strtolower

http://dventurin.wordpress.com


He uses strtolower because object's properties ARE CaSe-sEnSiTiVe, unlinke array alphanumeric keys which aren't.

That isn't needed though, but if an array has a key like "Item1", you'll have to use $object->Item1 and not $object->item1
weblivehelp
Site Admin
 
Posts: 6
Joined: Thu May 01, 2008 3:23 pm


Return to Web Development

Who is online

Users browsing this forum: No registered users and 1 guest

cron