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 6376393
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T01:44:19+00:00 2026-05-25T01:44:19+00:00

In PHP you can dynamically create variables $myarr = array(‘name’=>’Adam’, ‘age’=>22, ‘sex’=>’male’); foreach ($myarr

  • 0

In PHP you can dynamically create variables

$myarr = array('name'=>'Adam', 'age'=>22, 'sex'=>'male');
foreach ($myarr as $k=>$v)
    $$k = $v;

is there a way to pass a callback functionX and arrayX to another functionY, dynamically create variables from arrayX in functionY, and be able to reference those variables within functionX callback?

for example, I would like to:

function eachRecord($arr, $callback){
    foreach ($arr as $k=>$v) $$k = $v;
    $callback();
}
$myarr = array('name'=>'Adam', 'age'=>22, 'sex'=>'male');
eachRecord($myarr, function(){
   echo "{$name} is a {$sex} of age {$age}.";
});

i don’t want to have to pass variables back into the callback function, since i may not know the length or keys within the array, and i don’t want to pollute the global scope with unknown variable names because they are created dynamically.

is there any way to do this? closures?

thanks

  • 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-25T01:44:21+00:00Added an answer on May 25, 2026 at 1:44 am

    No, it’s not possible. It’s not hard to work around like this though:

    function callback($values) {
       extract($values);
       echo "{$name} is a {$sex} of age {$age}.";
    }
    
    callback($myArr);
    

    You’re essentially only reinventing http://php.net/extract anyway.

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

Sidebar

Related Questions

In PHP I can name my array indices so that I may have something
How can I dynamically invoke a class method in PHP? The class method is
Can PHP array passed POST method to catch in ASP.NET MVC? Query string: WebAccess/ArrayTest?val[]=1&val[]=2&val[]=3
A PHP array can have arrays for its elements. And those arrays can have
In PHP I can do the following: $myVar = 'name'; print $myClass->$myVar; // Identical
I'm currently using PHP to dynamically create a javascript which will be echoed on
I want to create subdomains dynamically through a PHP script or something similar. I've
I want to create a file on the webserver dynamically in PHP. First I
I'm using php. I'd like to know how can I dynamically export data to
I want to create PHP files dynamically. Static HTML code works fine, but when

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.