1

I want to create separate variables from the key of an array where variable is the key and the content is the value of that key in the array

3
  • If you want to use extract for this, be aware that the default mode is EXTR_OVERWRITE which poses the potential security risk of overwriting important existing variables. It is safer to use EXTR_PREFIX_ALL or not to use extract at all Commented Dec 8, 2010 at 8:23
  • @Gordon: thanks for the warning. i am just looking for a solution for a local migration script :D Commented Dec 8, 2010 at 8:25
  • 2
    (deleted a series of off-topic comments) Commented Dec 20, 2010 at 10:32

5 Answers 5

6

extract($array);


Example

<?php

$array = array('a' => 'abc', 'b' => 'def');

extract($array);

var_dump($a, $b);

// string(3) "abc"
// string(3) "def"

See it.

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

1 Comment

+1 for being the first to supply a working solution along with an example.
6

using this way
$data=array();

  $data["value1"]=3;

  $data["value2"]=4;



  $three = $data["value1"];

  $four = $data["value2"];

Comments

5

Use the extract() function for this.

$var_array = array("color" => "blue",
                   "size"  => "medium",
                   "shape" => "sphere");
extract($var_array);

which will give:

$color = 'blue'
$size = 'medium'
$shape = 'sphere'

Comments

4

use

 extract($array);

Comments

3

PHP's extract function will do exactly that.

Comments

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.