0
我有兩個變量:如何動態定義關聯數組鍵?
$keystr = 'plant,fruit,exotic';
$value='kiwi';
如何創建關聯數組?
$arr = ('plant'=>array('fruit'=>array('exotic'=>'kivi')));
我有兩個變量:如何動態定義關聯數組鍵?
$keystr = 'plant,fruit,exotic';
$value='kiwi';
如何創建關聯數組?
$arr = ('plant'=>array('fruit'=>array('exotic'=>'kivi')));
$keystr = 'plant,fruit,exotic';
$value='kiwi';
$arr = array();
$current = &$arr;
$keys = explode(',', $keystr);
foreach($keys as $key) {
$current[$key] = array();
$current = &$current[$key];
}
$current = $value;
unset($current);
var_dump($arr);