Php 如何将数组的第一个元素移动到最后一个?
我们必须在下面代码的最后一次检查中显示数组的第一个元素Php 如何将数组的第一个元素移动到最后一个?,php,arrays,Php,Arrays,我们必须在下面代码的最后一次检查中显示数组的第一个元素 public function getFilters(\Magento\Catalog\Model\Layer $layer) { if (!count($this->filters)) { $this->filters = [ $this->objectManager->create( $this->filterTypes[sel
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
如何移动它?您可以使用:
$data = array_shift($this->filters);
$this->filters[]= $data;
示例输出:-这取决于您是要交换第一个和最后一个元素,还是要从数组的末尾移除元素并将其前置到开头(实际上已将所有元素进一步移动)。 对于前者,您可以使用(如中所述) 对于后者,您可以使用:
$first_element = array_unshift($filters)
array_push($first_element)
@德维达斯很高兴帮助您:):)
$first_element = array_unshift($filters)
array_push($first_element)