skip to Main Content

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

public function getFilters(MagentoCatalogModelLayer $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] = MagentoCatalogSearchModelLayerFilterCategory
$this->filters[1] = MagentoCatalogSearchModelLayerFilterAttribute
$this->filters[2] = MagentoCatalogSearchModelLayerFilterAttribute
$this->filters[3] = MagentoCatalogSearchModelLayerFilterAttribute
$this->filters[4] = MagentoCatalogSearchModelLayerFilterAttribute

How to move it?

2

Answers


  1. You can use array_shift() for this:

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

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

    Login or Signup to reply.
  2. 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)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search