我在取消设置数据时遇到错误。任何人都可以告诉我该怎么做。这里我的列数组看起来像: -
enter code here
Array
(
[0] => Id
[1] => Name
[2] => MainDeity
[3] => Description
[4] => MainImage
[5] => Category
[6] => Tehsil
[7] => City
[8] => District
[9] => State
[10] => Terrain
) 我正在使用laravel框架我想取消三列Id,State,Terrain 我使用过此代码,但它显示错误: - 无法取消设置字符串偏移
enter code here
$columns = Schema::getColumnListing('places');
foreach ($columns as $key => $value) {
if($value=="Id"){
unset($value[$key]);
}
$columndata[] = $value;
}
echo "<pre>";print_r($columndata); die;
答案 0 :(得分:0)
应该是这样的,
$columns = Schema::getColumnListing('places');
foreach ($columns as $key => $value) {
if($value=="Id"){
unset($columns[$key]);
} else {
$columndata[] = $value;
}
}
echo "<pre>";print_r($columndata); die;
答案 1 :(得分:0)
检查此代码,
$arr = ["0" => "Id", "1" => "Name"
];
$columndata = [];
foreach ($arr as $key => &$value) {
if(in_array($value,["Id",'State','Terrain']){
unset($value);
}else{
$columndata[] = $value;
}
}
$columndata = array_values(array_filter($columndata));
print_r($columndata);
错误即将发生,因为在检查ID之后,您将取消该值。然后尝试在columndata中保存该值。
这是有效的code
答案 2 :(得分:0)
这是一个简单的单行:
$result = array_diff(Schema::getColumnListing('places'), ['Id', 'State', 'Terrain']);