Jump to content

Recursive function in a class


girish.kc

Recommended Posts

Hi,

 

I am using a separate class for all the functions. And using using the following function "objectToArray" to convert the JSON decoded object to Array.

 

Class file:

     


               class xyz
              {
                    . 
                    .
                    .
                    .
                    .

                    function objectToArray($d) {
	         if (is_object($d)) {
	 	     $d = get_object_vars($d);
        	         }
	         if (is_array($d)) {
		     return array_map(__FUNCTION__, $d);
	         }
	         else {
		     return $d;
	         }
          }
                    .
                    .
                    .

          }
      

 

 

Main page:

          $obj = new xyz();

          $arr = $obj->objectToArray($json_obj);

 

Because the function "objectToArray" is recursive, array_map(__FUNCTION__, $d) is not able to applies the callback. And throws the warning "array_map() expects parameter 1 to be a valid callback".

 

Need help!

 

Girish

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.