Tuesday, 28 June 2016

How to convert stdclass object to Array and Array To stdclass Object

//Object To Array

function object2Array($d)
{
if (is_object($d))
{
$d = get_object_vars($d);
}

if (is_array($d))
{
return array_map(__FUNCTION__, $d);
}
else
{
return $d;
}
}

//   After  This

  $attachments_id=$value -> attachments;
   $attachments_array = object2Array($attachments_id);
   foreach($attachments_array as $attachments_data)
   {
        echo "=====================Start Attachments  Data =================================<br/>";
echo "ID&nbsp; : ".$attachments_data['id']."<br/>";
echo "uploaded_name&nbsp; :".$attachments_data['uploaded_name']."<br/>";
echo "uploaded_extension&nbsp; : ".$attachments_data['uploaded_extension']."<br/>";
echo "mime_type&nbsp; : ".$attachments_data['mime_type']."<br/>";
echo "file_size&nbsp; :".$attachments_data['file_size']."<br/>";
echo "url&nbsp; : ".$attachments_data['url']."<br/>";
echo "=====================End Attachments Data =================================<br/>";
   }

// Array To Object

function array2Object($d)
{
if (is_array($d))
{
return (object) array_map(__FUNCTION__, $d);
}
else
{
return $d;
}
}

$object = array2Object($array);
print_r($object);
echo "\r\n";


Click  Here

No comments:

Post a Comment