有时,当我们事先不知道维度时,我们需要根据需要创建一个多维数组。假设我有下面的代码,我想在给定字符串中存在"_"的情况下创建一个多维数组。这里,这个给定的字符串是$table,我喜欢从中构造一个多维数组。
$table='Customer_Contacts';
$fi='City';
$fv='New York';
$explodedTableName=explode('_', $table);
$tobeEvaluatedArray="$association";
foreach($explodedTableName as $etn){
$tobeEvaluatedArray.="['$etn']";
}
$tobeEvaluatedArray.="['$fi']=$fv";
eval($tobeEvaluatedArray);
现在我想得到一个数组,即
$association['Customer']['Contacts']['City']='New York';
我想你想要这个动态。这应该会起作用,并给你一个定制的想法:
$table = 'Customer_Contacts';
$keys = explode('_', $table);
$keys[] = 'City';
$value = 'New York';
$temp = &$result;
foreach($keys as $key) {
$temp =& $temp[$key];
}
$temp = $value;
print_r($result);