我们必须将数组的第一个元素显示到最后一个Check below代码
public function getFilters(\Magento\Catalog\Model\Layer $layer)
{
if (!count($this->filters)) {
$this->filters = [
$this->objectManager->create(
$this->filterTypes[self::CATEGORY_FILTER],
['layer' => $layer]
),
];
foreach ($this->filterableAttributes->getList() as $attribute) {
$this->filters[] = $this->createAttributeFilter($attribute, $layer);
}
}
return $this->filters;
}
$ this-> filters的结果看起来像
$this->filters[0] = Magento\CatalogSearch\Model\Layer\Filter\Category
$this->filters[1] = Magento\CatalogSearch\Model\Layer\Filter\Attribute
$this->filters[2] = Magento\CatalogSearch\Model\Layer\Filter\Attribute
$this->filters[3] = Magento\CatalogSearch\Model\Layer\Filter\Attribute
$this->filters[4] = Magento\CatalogSearch\Model\Layer\Filter\Attribute
如何移动?
答案 0 :(得分:3)
您可以为此使用array_shift():
$data = array_shift($this->filters);
$this->filters[]= $data;
示例输出:-https://3v4l.org/WULpM
答案 1 :(得分:0)
这取决于您是要交换第一个元素还是最后一个元素,还是要从数组的末尾删除该元素并将其放在开头(有效地将所有元素移到了更远的位置)。 对于前者,您可以使用(如Swap array values with php所述)
$temp = $filters[0];
$a[0] = $filters[count($filters) - 1];
$a[1] = $temp;
对于后者,您可以使用:
$first_element = array_unshift($filters)
array_push($first_element)