On recursive methods, I usually add a $depth
parameter:
function my_function($depth = -1)
{
if ($depth === 0) {
return array(); // or any other value insisting an empty or false result
}
// do something
$result = array();
while ($some_condition) {
$result = array_merge($result, my_function($depth - 1)); // decrease $depth value
}
// maybe do something more
return $result;
}
When calling my_function() without a $depth
parameter, there won’t be any restriction in the depth. But if you provide a $depth
value, it’ll stop recursion when the $depth
value is zero.
Don’t use $depth--
in a loop. $depth - 1
is the correct way.
Related Posts:
- How to include checkbox in widget backend form?
- Using register_activation_hook in classes
- Upgrading PHP version results in “Use of undefined constant WP_CONTENT_DIR” warning?
- How to hide get_theme_mod if field empty
- renaming an admin menu item with decimal array index number
- How to get the original price of the product in woocommerce?
- How to make sure that only one wp_cron() runs at a time?
- How to create a custom config file and get data using inline JS in a wordpress page
- Replacing the NavWalker dropdown element
- Fatal error: Call to undefined function wpsc_cart_item_count()
- No wp-config.php file on local install of wordpress – site still displays
- Widgets not showing in my custom theme
- Edit category output
- integrating external php library into wordpress- the right way
- Getting the dropdown menu to redirect to different pages?
- To echo or not to echo?
- Different background-image by category
- enqueue multiple Google fonts with multiple weights and styles (italic)
- How to limit the content coming from wordpress shortcodes?
- What is the fastest way to load PHP functions that are only used in one theme template?
- Writing scripts using WordPress / WooCommerce classes?
- storing wp_head in a variable?
- Get taxonomy terms only of the WP_Query current posts
- Change description on specific WooCommerce product status change
- How do I attach a php file to an arbitrary slug and still have access to the wp-core functions?
- Woocommerce redirect thankyou page based on product ID doesn’t empty Cart
- Passing PHP Function Arguments from CLI vs URL
- tracking number field in Woocommerce order [closed]
- Is_template how to add one more template [closed]
- Open post-content in archive page in a Modal box with bootstrap
- Creating own shortcode – echoeing php variable based on JS variable
- How to modify URL (add GET values) after front end form submission?
- How to add a do_action on refreshing of WP customizer?
- Sort meta-value in the sequence array in pre_get_post
- Error: options page not found
- Filtering a Database Query
- anyway to put inside a $outprint=sprintf()
- Can’t save php string to a custom field
- Blob file download problem
- What does $_GET[‘iphone’] do?
- Appended comments but they show up above the content instead of below
- get_template_directory adding FTP root folders in urls
- What’s the best way to include PHP code in pages?
- Is there any wordpress plugin to upload images and captions to multiple pages once
- Removing the first 8-10 letters from a post?
- Line breaks inside shortcode variable
- Site Health says I’m running an older version of PHP even after I upgraded to 7.4.4
- WordPress REST API register_rest_route give a 500 error
- Where to insert redirect code based on http_referer?
- woocommerce wc_create_order(); is creating multiple orders instead of one?
- Override Admin menu icon
- Trying to get property of non-object in WordPress Breadcrumbs
- Delete database record using plugin from admin panel
- Post not populating for custom post type based on category selection
- How to display success message correctly and delete it when the page is refreshed
- Infinite Loop – WP_Query
- call_user_func_array() expects parameter 1 to be a valid callback, function ‘———-‘ not found or invalid function name
- How to display meta_value array?
- How can i make a search box to search by custom field
- ‘Bones’ theme: Load stock scripts in footer instead of header?
- go to home page when i select default in select-box
- Display Custom Field in Sidebar if Value is Present
- Database entry removed on browser refresh, Ajax PHP jQuery
- Hide specific product from Woocommerce if the logged-in user has already enrolled into the course
- How to find which .mo file is responsible for displaying a particular localized string?
- wp_Query with mutuplea values returns all posts
- is it possible for a URL to filter by tag
- Add Featured Image to Existing Post
- How to stop hiding buttons forward / backward in pagination?
- Preserving backslashes in post_content
- Add to cart quantity dynamic
- Infinite looping next post link within a certain category on a post
- update_post_meta not working, but values are true
- Change CSS Variable value in Theme Customizer Live Preview
- Importing the style and scripts in the plugin doesn’t work as well as in the theme
- gettint error 400 with AJAX
- Woocommerce textarea format ignored
- Windows Setup: Error establishing a database connection
- Pass product object to javscript
- Add Text Area To Child Theme’s Home Page
- Beginner Developing Theme with Bootstrap – NavMenu question
- remove blank space where sidebar was
- Using Tag Groups: Displaying groups and adjacent tags of current post
- Display 6 most recent post on homepage?
- Custom theme: adding unwanted tags in the content
- File path for attachments outside of wp-content/uploads
- Closed html class after counter = multiple of 3
- How to save the output of php variables used in inline css code to the main style.css?
- How to show all of aspecific post type that has taxonomy and a specific term
- WP Simple Fields – Single repeatable field inside repeatable field group
- get_permalink returning first letter
- Path for contact form file
- File Upload with Server in safe_mode
- WP plugin updates [duplicate]
- Custom Archive Page
- Load files content [closed]
- Shortcode for Visual Composer Grid fails turning string to integer
- Upgrade wordpress core programmatically with PHP [closed]
- Too many if’s and else if’s ?? – Must be better way [closed]
- Import js variables loaded via wp_localize_script() into js module without global scope connection