Array to string conversion help

hello,
I am attempting to make a round robin tournament schedule.

the code works $members,
but when I try to Select player column from database to fill array $players[].

I get Array to string conversion errors

**RoundRobinweb.php**
    <?php

    /**
     *
     * Create a round robin of teams or numbers
     *
     * @param    array    $players
     * @return    $array
     *
     */
     function roundRobin( array $players ){

        if (count($players)%2 != 0){
            array_push($players,"bye");
        }
        $away = array_splice($players,(count($players)/2));
        $home = $players;
        for ($i=0; $i < count($home)+count($away)-1; $i++)
        {
            for ($j=0; $j<count($home); $j++)
            {
                $round[$i][$j]["Home"]=$home[$j];
                $round[$i][$j]["Away"]=$away[$j];
            }
            if(count($home)+count($away)-1 > 2)
            {
                $s = array_splice( $home, 1, 1 );
                $slice = array_shift( $s  );
                array_unshift($away,$slice );
                array_push( $home, array_pop($away ) );
            }
        }
        return $round;
    }
    ?>

roundrobinweboutout.php

    <?php include "includes/db.php"; ?>
    <?php include "roundRobinweb.php"; ?>
    <?php

    // create an array of teams
    //$members = array('team1','team2','team3','team4', 'team5', 'team6', 'team7', 'team8', 'team9');

    $sql = "SELECT * FROM teams";
    $result = mysqli_query($connection, $sql);
    $players = array();
    if (mysqli_num_rows($result) > 0) {
    	while ($row = mysqli_fetch_assoc($result)){
    		$players[] = $row;
    	}

    }
    //foreach ($players[0] as $player){
    //	echo $player['player'];
    //}

    foreach ($players as $player){
    	echo "<br> " . $player['player'] . "<br> ";

    }
    // do the rounds
    //$rounds = roundRobin($members);
    $rounds = roundRobin($players);

    $table = "<table>\n";
    foreach($rounds as $round => $games){
        $table .= "<tr><th>Round: ".($round+1)."</th><th></th><th>Away</th></tr>\n";
        foreach($games as $play){
           $table .= "<tr><td>".$play["Home"]."</td><td>-vs-</td><td>".$play["Away"]."</td></tr>\n";
        }
    }
    $table .= "</table>\n";

    echo $table;
    ?>

OutPut:

Database connected
james

johns

petes

gregs

( ! ) Notice: Array to string conversion in C:\websites\schedule\roundRobinWebOutout.php on line 33
Call Stack

Time Memory Function Location

1 0.0023 403072 {main}( ) …\roundRobinWebOutout.php : 0

( ! ) Notice: Array to string conversion in C:\websites\schedule\roundRobinWebOutout.php on line 33
Call Stack

Time Memory Function Location

1 0.0023 403072 {main}( ) …\roundRobinWebOutout.php : 0

( ! ) Notice: Array to string conversion in C:\websites\schedule\roundRobinWebOutout.php on line 33
Call Stack

Time Memory Function Location

1 0.0023 403072 {main}( ) …\roundRobinWebOutout.php : 0

( ! ) Notice: Array to string conversion in C:\websites\schedule\roundRobinWebOutout.php on line 33
Call Stack

Time Memory Function Location

1 0.0023 403072 {main}( ) …\roundRobinWebOutout.php : 0

( ! ) Notice: Array to string conversion in C:\websites\schedule\roundRobinWebOutout.php on line 33
Call Stack

Time Memory Function Location

1 0.0023 403072 {main}( ) …\roundRobinWebOutout.php : 0

( ! ) Notice: Array to string conversion in C:\websites\schedule\roundRobinWebOutout.php on line 33
Call Stack

Time Memory Function Location

1 0.0023 403072 {main}( ) …\roundRobinWebOutout.php : 0

( ! ) Notice: Array to string conversion in C:\websites\schedule\roundRobinWebOutout.php on line 33
Call Stack

Time Memory Function Location

1 0.0023 403072 {main}( ) …\roundRobinWebOutout.php : 0

( ! ) Notice: Array to string conversion in C:\websites\schedule\roundRobinWebOutout.php on line 33
Call Stack

Time Memory Function Location

1 0.0023 403072 {main}( ) …\roundRobinWebOutout.php : 0

( ! ) Notice: Array to string conversion in C:\websites\schedule\roundRobinWebOutout.php on line 33
Call Stack

Time Memory Function Location

1 0.0023 403072 {main}( ) …\roundRobinWebOutout.php : 0

( ! ) Notice: Array to string conversion in C:\websites\schedule\roundRobinWebOutout.php on line 33
Call Stack

Time Memory Function Location

1 0.0023 403072 {main}( ) …\roundRobinWebOutout.php : 0

( ! ) Notice: Array to string conversion in C:\websites\schedule\roundRobinWebOutout.php on line 33
Call Stack

Time Memory Function Location

1 0.0023 403072 {main}( ) …\roundRobinWebOutout.php : 0

( ! ) Notice: Array to string conversion in C:\websites\schedule\roundRobinWebOutout.php on line 33
Call Stack

Time Memory Function Location

1 0.0023 403072 {main}( ) …\roundRobinWebOutout.php : 0

Round: 1 Away
Array -vs- Array
Array -vs- Array
Round: 2 Away
Array -vs- Array
Array -vs- Array
Round: 3 Away
Array -vs- Array
Array -vs- Array

You are selecting everything in the table. First off that is a bad thing.
You then take every row and stuff that array into the player array, hence your issue. If you only want the players, then select only the players in the query and when you assign it, say that is what you are assigning.

database

If you only want the players, then select only the players in the query

change
same errors.
say that is what you are assigning.???

not sure if I am doing this or not.

TY
astonecipher

When you do print_r($players) does it return something like this?

Array
(
    [0] => james
    [1] => johns
    [2] => petes
    [3] => gregs
)

This is my output below. I understand what you are trying to point out. I have an array inside of an array or called a multiple dimensional array, I was trying to find a tutorial of getting data from a database and put into an array, all I found is an example of multidimensional array tutorial. The example works fine. $members(team1, etc.) but I have 32 players, rather then list all the members I thought it best to extract the data from the database into an array. Now I understand the error,
Fixing is a problem:
removing this did not work $players = array(); putting players into the first array. maybe??
removing brackets here: $players[] = $row; did not work
I have tried other combinations of modifying this code. I am failing to get the desired results like yours of a single array.

Database connected
Array
(
[0] => Array
(
[player] => james
)

    [1] => Array
        (
            [player] => johns
        )

    [2] => Array
        (
            [player] => petes
        )

    [3] => Array
        (
            [player] => gregs
        )

)

and
Multiple Errors: Notice: Array to string conversion in C:\websites\schedule\roundRobinWebOutout.php on line 36

|Round: 1||Away|
|---|---|---|
|Array|-vs-|Array|
|Array|-vs-|Array|
|Round: 2||Away|
|Array|-vs-|Array|
|Array|-vs-|Array|
|Round: 3||Away|
|Array|-vs-|Array|
|Array|-vs-|Array|

I finally Got it!!!
$players[] = $row['player'];

IS this what you had in mind??? or is there a better solution and thank you for your hints.

Database connected

Array ( [0] => james [1] => johns [2] => petes [3] => gregs )

|Round: 1||Away|
| --- | --- | --- |
|james|-vs-|petes|
|johns|-vs-|gregs|
|Round: 2||Away|
|james|-vs-|johns|
|gregs|-vs-|petes|
|Round: 3||Away|
|james|-vs-|gregs|
|petes|-vs-|johns|

Yes that is what I was saying.

Sponsor our Newsletter | Privacy Policy | Terms of Service