2013-10-24 18 views
5
<?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> 

当我echo $信息数组它可以正常工作,但是当我回声$ player数组或$ $ pl_1变量或$结果数组值$ Notice to appear ... Array to string conversion and o/p不显示。 为什么?通知:数组到字符串转换在PHP

+1

因为两者都是数组而不是字符串。而不是'echo'使用print_r($ player [0]);和print_r($ pl_1);看阵列。 –

+0

你可以在任何变量上使用[var_dump](http://php.net/var_dump)来查看变量TYPE以及它的内容,以更好地理解你的代码中的变量赋值。 – Latheesan

+0

[参考 - 这个错误在PHP中意味着什么?](http://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php) – naththedeveloper

回答

10

尝试在开始处(第2行)用$player = array();代替$player[] = array();

这是因为你在这个变量的索引0处声明了一个数组,因为这个变量被告知是一个数组,因为[]。因此,您尝试在数组中放置一个数组,使其具有多维性。

8

你不能简单地echo一个数组。 echo只能输出字符串echo 'foo'很简单,它输出一个字符串。什么是echo应该完全在echo array('foo' => 'bar')的情况下?为了让echo在这里输出任何东西,PHP会将array('foo' => 'bar')转换为一个字符串,该字符串始终是字符串"Array"。而且由于PHP知道这可能不是你想要的,它会通知你。

问题是你想要像一个字符串对待数组。修复。

+9

一个不简单回显数组。 – Antoine