在PHP中给出这样的数组:
array('aaa_BC98E90D8ADA0',
'aab_KD9098DF90F',
...
'd5d_KDO90290393',
'nnn_OPDOE82993KD',
'kbh_Doodie0DDK',
...
);
如何拆分“_”周围的每个条目并使用右手标记并将每个条目分配给一个唯一的变量?
$aaa = 'BC98E90D8ADA0';
$aab = 'KD9098DF90F';
...
$d5d = 'KDO90290393';
$nnn = 'OPDOE82993KD';
$kbh = 'Doodie0DDK';
...
答案 0 :(得分:1)
$array_to_split = array(
'aaa_BC98E90D8ADA0',
'aab_KD9098DF90F',
'd5d_KDO90290393',
'nnn_OPDOE82993KD',
'kbh_Doodie0DDK',
);
foreach($array_to_split as $val) {
$var = explode($val);
$$var[0] = $var[1];
}
那应该动态设置var名称及其键,但我更喜欢这样做。
$new_array = array();
foreach($array_to_split as $val) {
$new_array[] = explode('_', $val);
}
echo $new_array['aaa']; // output, BC98E90D8ADA0
答案 1 :(得分:1)
以下是一个例子:
<?php
$array_to_split = array(
'aaa_BC98E90D8ADA0',
'aab_KD9098DF90F',
'd5d_KDO90290393',
'nnn_OPDOE82993KD',
'kbh_Doodie0DDK',
);
foreach ($array_to_split as $value) {
$temp = explode('_', $value);
if(count($temp)==2){
list($key,$val) = $temp;
$$key = $val;
}
}
echo $aaa; //BC98E90D8ADA0
?>