0

I am starting to work with PHP classes and have the following need:

I want to create a Class where I have 2 functions returning 1 array each. The 2 arrays can be used either withing the class (by other functions) or outside the class. How would I return the 3 arrays?

What I tried is to have other functions that just return the array value, so I can use it outside. Is this the most efficient way? Is there a better and quicker way to return my arrays independently?

So far I have something like this:

class playersData
{
    private $players = array();
    private $resultados = array();
    public function myPlayers ()
    {
        return $this->players();
    }

    public function playersQuery
    {
        $query = "SELECT * FROM scoreboard";
        $playersQuery = $con->prepare($query);
        $playersQuery->execute();
        $this->players = $playersQuery->fetchAll(PDO::FETCH_ASSOC);         
    }
    function checkPartidos 
      {
        $queryResultados = "SELECT * FROM partidos WHERE ganador <> 0";
        $result = $con->prepare($queryResultados);
        $result->execute();
        $this->$resultados = $result->fetchAll(PDO::FETCH_ASSOC);   
    }
3
  • Are you trying to return 3 individual arrays or are you trying to create a multiple array, with 3 arrays in one array like so: array[0][1] | array[0][2] | array[0][3]? Commented Jun 20, 2014 at 17:15
  • Ideally 3 individual arrays which I could access easily either within the Class or outside Commented Jun 20, 2014 at 17:15
  • What about instead of returning three arrays, it returns an object that holds three arrays? Or, accepts three aliases that can have an array assigned to them? Commented Jun 20, 2014 at 17:45

1 Answer 1

1

You can't return 3 values at the same time. In case of variables you can use:-

return $a.'|'.$b.'|'.$c;

then explode the string by |.

In case of arrays you can use:-

function testFunction() {
  $newArray = array();
  $newArray[] = array('a1', 'a2');
  $newArray[] = array('b1', 'b2');

  return $newArray;
}

Create dynamic array of you arrays and return the one. Please try.

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

1 Comment

As pe my understanding there is no other way to return multiple values at the same time.

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.