1

We have to show the first element of the array to the last Check below code

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;
}

The result of $this->filters will look like

$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

How to move it?

0

2 Answers 2

4

You can use array_shift() for this:

$data = array_shift($this->filters);
$this->filters[]=  $data;

Sample output:- https://3v4l.org/WULpM

Sign up to request clarification or add additional context in comments.

Comments

0

It depends, do you want to swap the first and last element, or would you want to remove the element from the end of the array and prepend it to the beginning (effectively having moved all elements on further). For the former, you could use (as mentioned at Swap array values with php)

$temp = $filters[0];
$a[0] = $filters[count($filters) - 1];
$a[1] = $temp;

For the latter you could use:

$first_element = array_unshift($filters)
array_push($first_element)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.