<body><script type="text/javascript"> function setAttributeOnload(object, attribute, val) { if(window.addEventListener) { window.addEventListener('load', function(){ object[attribute] = val; }, false); } else { window.attachEvent('onload', function(){ object[attribute] = val; }); } } </script> <div id="navbar-iframe-container"></div> <script type="text/javascript" src="https://apis.google.com/js/platform.js"></script> <script type="text/javascript"> gapi.load("gapi.iframes:gapi.iframes.style.bubble", function() { if (gapi.iframes && gapi.iframes.getContext) { gapi.iframes.getContext().openChild({ url: 'https://www.blogger.com/navbar.g?targetBlogID\x3d5235279\x26blogName\x3druff+rants\x26publishMode\x3dPUBLISH_MODE_BLOGSPOT\x26navbarType\x3dBLACK\x26layoutType\x3dCLASSIC\x26searchRoot\x3dhttps://steelburn.blogspot.com/search\x26blogLocale\x3den_GB\x26v\x3d2\x26homepageUrl\x3dhttp://steelburn.blogspot.com/\x26vt\x3d-830537559292416964', where: document.getElementById("navbar-iframe-container"), id: "navbar-iframe" }); } }); </script>
 

array2object() function

This is something that I don't want to lose, after cracking my head for hours, hence it's placed here.
This is a function which I couldn't find on the Net, a smart function to convert arrays to objects while retaining itself as arrays if necessary:

<?php
/**
* Just process non-numeric arrays. If the array has both numeric and string arrays,
* then remove numeric ones while retaining string arrays. Recurse into objects as well
* to check for any possible conversion.
**/
function array2object ($arg_array) {
    if (is_array($arg_array)) {
        $keys = array_keys($arg_array);
        foreach ($keys as $key) {
            if (is_numeric($key)) $has_number = true;
            if (is_string($key)) $has_string = true;       
            if (is_object($key)) $has_object = true;   
            };
        if (isset($has_number) and !isset($has_string)) {
            foreach ($arg_array as $key=>$value) {
                $tmp[] = array2object($value);
                }
            }
        elseif (isset($has_string)) {
            foreach ($arg_array as $key=>$value) {
                if (is_string($key))
                    $tmp->$key = array2object($value);
                }
            }
        } elseif (is_object($arg_array)) {
            foreach ($arg_array as $key => $value)
                if (is_array($value) or is_object($value))
                    $tmp->$key = array2object($value);
                    else
                    $tmp->$key = $value;
           }
        else {
            $tmp = $arg_array;
        };
    return $tmp;    //return the object
};
?>

Phew!

0 Comments:

Post a Comment

<< Home

What's going on around me... what's I feel about things happening around.. and also about upcoming events sometimes.

blogsphere
10 recent rants



Technorati search