2013-10-24 17 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