Hello All I have a multiple array. I am using a code like this.

it work some time when a test it without in a loop. When I call this in a loop It shows an internal server in ajax call. and does not give me any response.

         function sortarraybykey_returns($oldarray)
                   {


  if (count($oldarray) > 1)
    {

        function cmpreturns($a, $b)
        {
            if ($a->staff_distance_value < $b->staff_distance_value)
            {
                return -1;
            } elseif ($a->staff_distance_value == $b->staff_distance_value)
            {
                return 0;
            } else
            {
                return 1;
            }
        }

        usort($oldarray, "cmpreturns");
    }

    return $oldarray;
}

 Array
(
    [0] => stdClass Object
        (
            [ID] => 1
            [name] => Mary Jane
            [count] => 420
        )

    [1] => stdClass Object
        (
            [ID] => 2
            [name] => Johnny
            [count] => 234
        )

    [2] => stdClass Object
        (
            [ID] => 3
            [name] => Kathy
            [count] => 4354
        )
有帮助吗?

解决方案

You've defined a function within a function. As a function can only be defined once, calling sortarraybykey_returns a second time will result in a fatal error (Fatal error: Cannot redeclare cmpreturns()). This is most likely the issue you have. Try defining cmpreturns outside of sortarraybykey_returns.

function cmpreturns($a, $b)
{

  if ($a->staff_distance_value < $b->staff_distance_value)
  {
      return -1;
  }

  elseif ($a->staff_distance_value == $b->staff_distance_value)
  {
      return 0;
  }

  else
  {
      return 1;
  }

}

function sortarraybykey_returns($oldarray)
{

  if (count($oldarray) > 1)
  {
    usort($oldarray, "cmpreturns");
  }

  return $oldarray;

}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top