Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 208
HungryDB Avatar asked Oct 24 '13 11:10

HungryDB


2 Answers

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.

like image 99
Guillaume Chevalier Avatar answered Nov 16 '22 18:11

Guillaume Chevalier


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.

like image 9
deceze Avatar answered Nov 16 '22 19:11

deceze