Viking gear
Gear Gifts and Apparel
Evil gear
Christmas is almost here
Goth and Gore Gifts
Shop right now or else!
PHP Gallery
PHP Gallery is a free php image script.

here are some of the best and useful php scripts and snippets to help in your projects. Php displayed below. Use the search for specific script lookups. Click the category links to view scripts in Javascript and cgi. To add your own script click the link and add your useful script example.

Convert an array into object values and objects into arrays

convert array objects
arrays-and-objects date added Jan 2013

<?php
function array2object($array) {
 
    if (
is_array($array)) {

        
$obj = new StdClass();
 
        foreach (
$array as $key =
$val){
            
$obj->$key $val;
        }
    }
    else {
 
$obj $array; }
 
    return 
$obj;
}
 
function 
object2array($o
bject
) {
    if (
is_object($object)) {
        foreach (
$object as $
key => $value) {
            
$array[$key] = $value;
        }
    }

    else {
        
$array $object;
    }
    return 
$array;
}

 
 
// example:
 
$array = array('foo' => 'bar''one' => 'two'
'three' => 'four');
 
$obj array2object($array);
 
print 
$obj->o
ne
// output's "two"
 
$arr object2array($obj);
 
print 
$arr['f
oo'
]; // output's bar

?>


Add your comment.













No comments yet

Search ScriptsnTips


Php JavaScripts CGI/Perl