我想通过点分隔键删除特定的子阵列。下面是一些工作(是它的工作,但甚至还没有接近一个很好的解决方案)代码:通过点分隔键删除多维数组中的子树
$Data = [
'one',
'two',
'three' => [
'four' => [
'five' => 'six', // <- I want to remove this one
'seven' => [
'eight' => 'nine'
]
]
]
];
# My key
$key = 'three.four.five';
$keys = explode('.', $key);
$str = "";
foreach ($keys as $k) {
$sq = "'";
if (is_numeric($k)) {
$sq = "";
}
$str .= "[" . $sq . $k . $sq . "]";
}
$cmd = "unset(\$Data{$str});";
eval($cmd); // <- i'd like to get rid of this evil shit
对这个更漂亮的解决方案的任何想法?
谢谢,这工作得很好:) – Link