user3733831 user3733831 - 1 month ago 13
PHP Question

How to create variables using a php array?

I have a PHP array, something like below:

Array(

[0] => Array
(
[name] => month_year
[value] => 201609
)

[1] => Array
(
[name] => advance_amount
[value] => 50%
)

)


Using this array, I want to create 2 variables like this:

$month_year = '201609';
$advance_amount = '50%';


Can anybody tell me is this possible in php?

I tried it using two
foreach
but I don't have any idea how to precced.

foreach ($_POST as $k => $data) {
//echo "<pre>", print_r($data)."</pre>";
foreach ($data as $key => $value) {
echo $key."<br>";
}
}

Answer

Use variable variables. Your foreach loop should be like this:

foreach ($_POST as $k => $data) {
    $$data['name'] = $data['value'];
}

// display variables
echo $month_year . "<br />";
echo $advance_amount;
Comments