【问题标题】:php laravel array_merge key valuephp laravel array_merge 键值
【发布时间】:2015-07-21 14:55:10
【问题描述】:

您好,我正在创建一个数组,用于在我的站点中存储部分。其中一些部分将无法供某些用户查看,因此我需要在放入我的阵列之前检查相关用户的权限。但是,当我执行此 if 语句时,我会在一个我不想要的数组中得到一个数组,这会导致以下错误:

Method Illuminate\View\View::__toString() must not throw an exception

这是我正在使用的代码:

$user = Auth::user(); 
 if(($user->hasRole('Admin') || $user->hasRole('Admin') || $user->hasRole('Project Master') || $user->hasRole('Project Owner'))) {
  $restrictsections = ['Create' => route('project.create'),
                       'Sort' => route('project.sort'),];
  }

$this->sections = [
    'Projects' => [
        'View' => route('project.index'),
        $restrictsections

    ]
];

数组现在的结构如下:

array(1) {
  ["Projects"]=>
  array(2) {
    ["Create"]=>
    string(30) "http://projects.local/projects"
    [0]=>
    array(2) {
      ["Create"]=>
      string(37) "http://projects.local/projects/create"
      ["Edit"]=>
      string(35) "http://projects.local/projects/sort"
    }
  }
}

相对于:

  $this->sections = [
        'Project' => [
            'View' => route('project.index'),
            'Create' => route('project.create'),
             'Sort' => route('project.sort'),
        ]
    ];


array(1) {
  ["Project"]=>
  array(3) {
    ["View"]=>
    string(30) "http://projects.local/project"
    ["Create"]=>
    string(37) "http://projects.local/project/create"
    ["Sort"]=>
    string(35) "http://projects.local/project/sort"
  }
}

任何想法如何将两个数组合并在一起?但它的结构应该如下:

array(1) {
  ["Project"]=>
  array(3) {
    ["View"]=>
    string(30) "http://projects.local/project"
    ["Create"]=>
    string(37) "http://projects.local/project/create"
    ["Sort"]=>
    string(35) "http://projects.local/project/sort"
  }
}

【问题讨论】:

  • 你可以试试array_merge_recursive
  • 请回显“
    ”.print_r($array);太难读了……
  • @rohitarora nope 没用。还是同一个数组。

标签: php arrays laravel


【解决方案1】:

您可以使用+ 运算符来组合数组。

例如:

php > print_r(['View' => '1'] + ['Create' => 'two', 'Sort' => '3']);
Array
(
    [View] => 1
    [Create] => two
    [Sort] => 3
)

应用于您的代码:

$user = Auth::user(); 
 if(($user->hasRole('Admin') || $user->hasRole('Admin') || $user->hasRole('Project Master') || $user->hasRole('Project Owner'))) {
  $restrictsections = ['Create' => route('project.create'),
                       'Sort' => route('project.sort'),];
  }

$this->sections = [
    'Projects' => [
        'View' => route('project.index')
    ] + $restrictsections
];

编辑:+ 在技术上是一个联合,因此如果第二个数组的键存在于第一个数组中,它们将被忽略。

【讨论】:

  • 谢谢,如果用户没有这些角色中的任何一个,我会收到此错误Unsupported operand types,任何想法为什么以及如何避免此错误??
  • 在 if 语句之前将 $restrictsections 初始化为一个空数组,这样就不会假定为 null
【解决方案2】:

再创造一点

$this->sections = ['Projects' =>  $restrictsections];
$this->sections['Projects']['View'] = route('project.index');

【讨论】:

    【解决方案3】:

    像这样使用array_merge()

    $this->sections = [
        'Projects' => array_merge(
            ['View' => route('project.index')],
            $restrictsections
        )
    ];
    

    或像这样使用+ 运算符

    $this->sections = [
        'Projects' => ['View' => route('project.index')] + $restrictsections
    ];
    

    【讨论】:

      猜你喜欢
      • 2012-07-23
      • 2011-08-21
      • 1970-01-01
      • 2010-10-22
      • 2012-09-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多