notice: array to string conversion in php

76,500

Solution 1

Try replacing $player[] = array(); by $player = array(); at the beginning (line 2).

This is because that you declare an array at the index 0 of this variable which is told to be an array because of the []. You therefore try to place an array in your array, making it multidimensional.

Solution 2

You cannot simply echo an array. echo can only output strings. echo 'foo' is simple, it's outputting a string. What is echo supposed to do exactly in the case of echo array('foo' => 'bar')? In order for echo to output anything here, PHP will convert array('foo' => 'bar') to a string, which is always the string "Array". And because PHP knows this is probably not what you want, it notifies you about it.

The problem is you're trying to treat an array like a string. Fix that.

Share:
76,500
HungryDB
Author by

HungryDB

Updated on November 17, 2020

Comments

  • HungryDB
    HungryDB over 3 years
    <?php       
     $player[] = array();
        $team_id = $_SESSION['tid'];
    
        $team_pids = $con->prepare("SELECT p_id FROM players_to_team WHERE t_id = ?");
    
        $team_pids->bindParam(1,$team_id);
    
        $team_pids->execute();
    
        while($info = $team_pids->fetch(PDO::FETCH_ASSOC))
        {
                $player[] = $info['p_id'];
                echo $info['p_id'];
        }
        $pl_1 = $player[0];
        .
            .
            .
        $pl_10 = $player[9];
    
        echo $player[0]; //notice here
        echo $pl_1;      //notice here
    ?>
    <table>
    
    $query = $con->prepare("SELECT role,name,value FROM players WHERE p_id = '".$pl_1."'");
    // notice here
                   $query->execute();
    
                   while($result = $query->fetch(PDO::FETCH_ASSOC))
                   {
                         echo "<tr>";  
                         echo "<td>".$result['role']."</td>";
                         echo "<td>".$result['name']."</td>";
                         echo "<td>".$result['value']."</td>";
                }
    ?>
    </tr>
    </table>
    

    when i echo $info array it works fine, but when i echo $player array or $pl_1 variable or $result array values Notice appears...Array to string conversion and o/p doesn't show. why?

  • Anthony
    Anthony almost 9 years
    One does not simply echo an array.