Thread: array to object
View Single Post
Old 03-24-2008, 01:31 AM   #1 (permalink)
Gibou
The Contributor
 
Gibou's Avatar
 
Join Date: Nov 2007
Location: France, near Paris
Posts: 53
Thanks: 6
Gibou is on a distinguished road
Default array to object

Hi !

I would like to quickly transform an array into an object and whatever the keys presents into the array. I want it dynamic and not static, key by key.
I've thought to this method:

PHP Code:
<?php
foreach($params as $key => $val)
{
    if(isset(
$this->$key))
        
$this->$key $val;
}
?>
but it doesn't work. It cause no error but the $this->$key never exists.
My array is like:
id => 1
title => "test Site"
url => "http://www.monsite.com"
story => "blablabla"

and my object attributes are:

PHP Code:
<?php
private $id;
private 
$title;
private 
$url;
private 
$story;
?>
Do you have any idea ?

Thank you !
__________________
Wedus project's Website
Send a message via MSN to Gibou
Gibou is offline  
Reply With Quote