简体   繁体   中英

notice: array to string conversion in php

<?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?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM