我如何取消 php 中的多个键
how do i unset multiple keys in php
我在取消设置数据时遇到错误。谁能告诉我该怎么做。这里我有列数组看起来像:-
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;
应该是这样的,
$columns = Schema::getColumnListing('places');
foreach ($columns as $key => $value) {
if($value=="Id"){
unset($columns[$key]);
} else {
$columndata[] = $value;
}
}
echo "<pre>";print_r($columndata); die;
检查此代码,
$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 后,您正在取消设置该值。然后尝试将该值保存在列数据中。
这里正在工作code
这是一个简单的单行代码:
$result = array_diff(Schema::getColumnListing('places'), ['Id', 'State', 'Terrain']);
我在取消设置数据时遇到错误。谁能告诉我该怎么做。这里我有列数组看起来像:-
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;
应该是这样的,
$columns = Schema::getColumnListing('places');
foreach ($columns as $key => $value) {
if($value=="Id"){
unset($columns[$key]);
} else {
$columndata[] = $value;
}
}
echo "<pre>";print_r($columndata); die;
检查此代码,
$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 后,您正在取消设置该值。然后尝试将该值保存在列数据中。
这里正在工作code
这是一个简单的单行代码:
$result = array_diff(Schema::getColumnListing('places'), ['Id', 'State', 'Terrain']);