如何在PHP中取消多个键的设置

3

在取消数据时,我遇到了错误。有人能告诉我该如何做吗?这是我列出的数组:

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。我已经使用了这段代码,但是它显示错误:Cannot unset string offsets
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;

你想通过检查其值来取消设置键吗?正确吗? - Mittul At TechnoBrave
是的,我想通过它的值取消设置。 - user6384346
请检查以下链接:https://dev59.com/jXA75IYBdhLWcg3w3NLf - Mittul At TechnoBrave
3个回答

3

这是一个简单的一行代码:

$result = array_diff(Schema::getColumnListing('places'), ['Id', 'State', 'Terrain']);

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;

我认为当我打印columnsarray时,它会正常运行。 - user6384346

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后,您取消了该值。然后尝试将该值保存在列数据中。
这里是可行的代码

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接