1
所以我有,我想,以填補倍數空數組定義的數組,讓我們說訪問的「指示器」
$a_tab = [];
$b_tab = [];
$c_tab = [];
我有一個包含一些數據的數組,讓我們說
$data = ['a' => 'foo', 'b' => 'bar', 'c' => 'foobar'];
我希望能夠做到這樣的事情:
foreach($data as $key => $value) {
$tab_name = $key . '_tab'; // so we have a variable that have the same name as one of the array we declared before
$$tab_name[$value] = $value; // this should add 'foo' into $a_tab, 'bar' in $b_tab and 'foobar' in $c_tab
}
但是沒有值添加到任何數組...
有人能解釋我我做錯了什麼?
PS:如果你不想僞代碼,這裏是我所面臨的問題,我有代碼:
// $tab is a parameter of the current function
$done_courses = []; // the array where we are going to put every courses that already have been added in one bifurcation tab
$regex_wz = '/\_werkzoekende/';
$regex_bd = '/\_bediende/';
$regex_op = '/\_outplacement/';
$bifurcation_keys = ['wz_tab' => $regex_wz, 'bd_tab' => $regex_bd, 'op_tab' => $regex_op];
// create the 3 arrays
$wz_tab = [];
$bd_tab = [];
$op_tab = [];
foreach($tab as $key => $value) {
foreach($bifurcation_keys as $tab_name => $regex) {
if(preg_match($regex, $key)) {
$n_k = preg_replace($regex, '', $key);
$$tab_name[$n_k] = $value;
if(!isset($done_courses[$n_k])) {
$done_courses[$n_k] = $n_k;
}
}
}
}
是的,就是這樣,我剛纔想出了這個...... 感謝您的回答:) 你知道'$$'的用法是什麼嗎? –
我把它叫做'參考參考變量' –
參考更像'&$變量' –