no_freedom
no_freedom

Reputation: 1961

Convert associative array to individual variables named by keys

I have array in following format. I want to convert it into another format like below.

Array
(
 [b2] => 2
 [b3] => 8
 [b4] => 2
 [b5] => 4
)

Is it possible key is converted into PHP variables and its value automatically assigned to variables? Is it possible?

$b2 = 2;
$b3 = 8;
$b4 = 2;
$b5 = 4;

Upvotes: 0

Views: 245

Answers (2)

Keith Gaughan
Keith Gaughan

Reputation: 22675

You're looking for the extract() function. You pass in the array you want to extract into the current scope, and that's it. Mind though that you should be very careful when you use this function; for safety, pass EXTR_SKIP in as the second argument to prevent overwriting existing variables, after all you wouldn't want the likes of $_SESSION or $_SERVER throttled by an array with malicious data...

Upvotes: 0

user557846
user557846

Reputation:

looks like you want extract()

Upvotes: 6

Related Questions