Using json_encode on objects in PHP
        Posted  
        
            by 
                Alan
            
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by Alan
        
        
        
        Published on 2011-01-15T02:46:31Z
        Indexed on 
            2011/01/15
            2:53 UTC
        
        
        Read the original article
        Hit count: 240
        
Hi, I'm trying to output lists of objects as json and would like to know if there's a way to make objects usable to json_encode?  The code I've got looks something like
$related = $user->getRelatedUsers();
echo json_encode($related);
Right now, I'm just iterating through the array of users and individually exporting them into arrays for json_encode to turn into usable json for me.  I've already tried making the objects iterable, but json_encode just seems to skip them anyway.
© Stack Overflow or respective owner