在foreach循环中取消设置多维数组

时间:2016-03-04 10:41:17

标签: php arrays multidimensional-array

我正在从数据库中的表中检索所有行,以便在数据表客户端中显示它们,并显示数据和相应的操作。这是不相关的,但我正在使用laravel。这就是我在做的事情:

$allItems = $model::withTrashed()->get();
$allRows = array();
foreach ($allItems as $item) {

        $allRows[] = array(
            $item->id,
            $item->name,
            $item->category->name,
            $item->url,
            strftime('%d-%m-%Y  %H:%M', $item->updated),
            'actions' => array(
                'view' => array(
                    'href' => $this->_mainTable. '/view/' .$item->id,
                ),
                'delete' => array(
                    'href' => $this->_mainTable. '/delete/' .$item->id,
                    'modal' => array(
                        'question' => 'Delete job:',
                        'name' => $item->name. '?',
                        'advice_phrase' => '',
                        'btn_confirm' => 'Delete',
                        'class_phrase' => '',
                    ),
                ),
            ),
        );

        if($item->trashed()) {
            unset(end($allRows)['actions']['delete']);
            dd(end($allRows)['actions']);
        }
}

这不是给我任何错误,而是var_dum() - > (dd(end($allRows)['actions'])),在if内部仍显示删除操作。我不明白为什么这不起作用。

3 个答案:

答案 0 :(得分:1)

试试这样。

$allItems = $model::withTrashed()->get();
$allRows = array();
foreach ($allItems as $item) {
    $delete = array();
    if (!$item->trashed()) {
        $delete = array(
            'action_table' => $this->_mainTable,
            'href' => $this->_mainTable . '/view/' . $item->id,
            'modal' => array(
                'question' => 'Delete job:',
                'name' => $item->name . '?',
                'advice_phrase' => '',
                'btn_confirm' => 'Delete',
                'class_phrase' => '',
            ),
        );
    }

    $allRows[] = array(
        $item->id,
        $item->name,
        $item->category->name,
        $item->url,
        strftime('%d-%m-%Y  %H:%M', $item->updated),
        'actions' => array(
            'view' => array(
                'action_table' => $this->_mainTable,
                'href' => $this->_mainTable . '/view/' . $item->id,
            ),
            'delete' => $delete,
        ),
    );
}

答案 1 :(得分:0)

让我们看看我是否理解。你想只显示动作视图吗?如果是这样,请不要将操作删除添加到数组$ allItems。

答案 2 :(得分:0)

这种看法多余,我已经能够实现这个目标:

RewriteEngine On

RewriteCond %{THE_REQUEST} /abc\.php[\s?] [NC]
RewriteRule ^ /abc.html [R=301,L]

RewriteRule ^abc\.html$ abc.php [NC,L]