10

PHP has a function extract that will convert an array like this:

$array = array(
 'var1' => 1,
 'var2' => 2
);

to:

$var1 = 1;
$var2 = 2;

now, I need the opposite, i have few variables:

$var3 = 'test';
$test = 'another';
$datax = 1;

that needs to be:

$array = array(
 'var3' => 'test',
 'test' => 'another',
 'datax' => 1
);

Is there something like this in PHP?

4
  • 1
    If these are global scope, you already have it in $GLOBALS['test']. $GLOBALS['var3'] $GLOBALS reference Commented Feb 21, 2012 at 14:48
  • 3
    It says compact right on the manual page for extract. Why didnt you find it? Commented Feb 21, 2012 at 14:53
  • The correct answer would be "create a class containing the properties var3, test and datax", but unfortunately in SO perspective that won't be an answer to your question. Commented Feb 21, 2012 at 14:57
  • (For beginners wandering here.) No, the "correct" answer to this particular question -- i.e. without being opinionated, or "smart", assuming various background conditions that are not actually present... -- is absolutely not to "create a class", or to involve any other concepts or complexities into the problem space that have neither been called for nor necessary. And it has nothing to do with SO (but everything with Occam's razor and general conceptual hygiene). (If the question or the context were about architecture, OOP, or coding best practices etc., CodeCaster would've had a point.) Commented Mar 22, 2018 at 15:37

4 Answers 4

24

You can use compact() to achieve this.

$var3 = 'test';
$test = 'another';
$datax = 1;
$array = compact('var3', 'test', 'datax');

Reference: http://php.net/manual/en/function.compact.php

Sign up to request clarification or add additional context in comments.

Comments

4

like this

$preDefined = (get_defined_vars());
$var3 = 'test';
$test = 'another';
$datax = "1";
$newDefined = array_diff(get_defined_vars(), $preDefined);
print_r($newDefined);

Comments

2
$array = get_defined_vars()

See get_defined_vars()

Comments

1

You'd have to be really sure you wanted to do this (it includes things in the global scope automatically) but you can use

$my_vars = get_defined_vars();

If you want it more selective than that, you could look at filtering it like this:

$my_vars = pack_vars(get_defined_vars())

function pack_vars ($defined_vars)
{
    $packed = array();
    $ignored = array('dont_use_this', 'ignored_var', 'ignore_this_too');

    foreach ($defined_vars AS $key => $value)
    {
        if (!in_array($key, $ignored))
        {
            $packed[$key] = $value;
        }
    }
    return $packed;
}

1 Comment

Inside of a function get_defined_vars() returns only those variables available in the function. I have used frameworks previously which expect an array to be returned from the controller. This function is useful. Remember to unset any unwanted variables before using it - such as a database result set.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.