I got a PHP multidimensional array, I want to create variables from it first element as variable name and the second element as the variable value. I want to use this logic to print create the variables based on the language selected, the first column always will have the same names but the second value will generate different strings based on the language selected.
Array
(
[0] => Array
(
[0] => el1
[1] => Grouping
)
[1] => Array
(
[0] => el2
[1] => Type
)
[2] => Array
(
[0] => el3
[1] => Starting Date
)
[3] => Array
(
[0] => el4
[1] => Ending Date
)
[4] => Array
(
[0] => el5
[1] => Section
)
[5] => Array
(
[0] => el6
[1] => Cell
)
[6] => Array
(
[0] => el7
[1] => Client
)
[7] => Array
(
[0] => el8
[1] => Status
)
[8] => Array
(
[0] => el9
[1] => Article
)
[9] => Array
(
[0] => el10
[1] => Search
)
)
I want to assign the [0]
value as a variable name and [1]
as the variable value, the declaration should be in this way related to my array presented before:
<?php
el1="Grouping";
el2="Type";
el3="Starting Date";
?>
... and so on.
I want to echo out on the HTML page the string from the variable.
Try this :
foreach ($array as $index => $subarray) {
${$subarray[0]} = $subarray[1];
}
Test :
$array = array(
0 => array(
0 => "test",
1 => "value"
),
1 => array(
0 => "test2",
1 => "value2"
)
);
foreach ($array as $index => $subarray) {
${$subarray[0]} = $subarray[1];
}
var_dump($test, $test2);
The output is :
string 'value' (length=5)
string 'value2' (length=6)
A simple loop through the array should do the trick:
foreach ($data as $item) {
$temp = $item[0];
${$temp} = $item[1];
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.