Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6736687
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:09:34+00:00 2026-05-26T11:09:34+00:00

I want to apply a function to each element/prop of an object but it

  • 0

I want to apply a function to each element/prop of an object but it seems array_walk_recursive() does not work on object. i.e:

if( $re = $con->query("SELECT id, created_date, contents FROM " . 
                      POST_DATA . " WHERE type = 'news' ORDER BY ".
                      "created_date DESC LIMIT $amount") ) {
    if( $re->num_rows != 0 ) {
        while( $ob = $re->fetch_object() ) {
            $ob = array_walk_recursive( $ob, "_output" );
            print_r($ob);
            die();
        }
    }
}

would simply return ‘1’.

How might I resolve this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T11:09:35+00:00Added an answer on May 26, 2026 at 11:09 am

    It’s actually returning a value of True for array_walk_recursive. If you look at the function’s documentation, you’ll see that what this method is doing is calling the function _output for each item and key in the object.

    You should also have some code that looks similar to this, I would imagine, to get it to work correctly:

    function _output($data, $key) {
        echo "For the key $key, I got the data: ";
        print_r($data);
    }
    

    Where _output is called because that is the stringified name that you gave in the array_walk_recursive function. That should print your values to the screen.


    Edit:

    It seems that I’m not actually answering what you were originally wanting to do, though. If you’re wanting to apply a function to every element of an array, I would suggest that you look at array_map. You can use array_map like this:

    function double($item) {
        return 2 * $item;
    }
    
    array_map('double', $item);
    

    Ultimately, if the recursion is something that you desire, you could probably do something like this:

    function callback($key, $value) {
        // do some stuff
    }
    
    function array_map_recursive($callback, $array) {
       $new_array = array()
       foreach($array as $key => $value) {
          if (is_array($value)) {
              $new_array[$key] = array_map_recursive($callback, $value);
          } else {
              $new_array[$key] = call_user_func($callback, $key, $value);
          }
       }
       return $new_array;
    }
    
    array_map_recursive('callback', $obj);
    

    That would return another array like $obj, but with whatever the callback was supposed to do.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to apply a function to each element of a cell array --
I want to select each of element below and apply multiple jquery commands to
I want to apply a function f to a list of values, however function
I want apply margin property for my html newsletter But Gmail ignores this CSS
I want to apply output cache programmatically to a particular control. But when I'm
What if you want to apply a function other than format to a list
I am attempting to apply a function to a dataset for each combination of
I have two matrices that I want to apply a function to, by rows:
Using Mathematica, I have a list: l={0,0,0,1,2,0,0,0,1,0,0,0,2,0,0,0} I want to apply a function to
I have a list and I want to apply a logical test to each

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.