1

I have a very basic repeatable field where I am allowing the user to enter some field along with an input where they can enter the 'Display Order' [1,2,3,4,5]. But I am struggling with how I could apply any sorting function on the data that is stored for the repeatable field. A sample output is the following:

stdClass Object
(
    [order] => Array
    (
        [0] => 1
        [1] => 3
        [2] => 2
    )
    [url] => Array
    (
        [0] => /first
        [1] => /third
        [2] => /second
    )
)

How would I go about so that when the module content is displayed in the front end; it would be based on the order defined by user [1,2,3] and not as it is currently [1,3,2].

1

Here is a sample function that can give you ordered list based on your order array.

public function getSortedArray($myObj, $ordering)
{
  // first build an array with keys as your order, values as data (url)
  $myArray = array();
  foreach($myObj->order as $i=>$order)
  {
    $myArray[$order] = $myObj->url[$i];
  }

  // now you can get the data as per the user requested order
  $return = array();
  foreach($ordering as $order)
  { 
    $return[] = $myArray[$order];
  }

  return $return;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.