简体   繁体   English

注意:php中的数组转换为字符串

[英]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. 当我回显$ info数组它工作正常,但当我回显$ player数组或$ pl_1变量或$结果数组值注意出现...数组到字符串转换和o / p不显示。 why? 为什么?

Try replacing $player[] = array(); 尝试替换$player[] = array(); by $player = array(); by $player = array(); at the beginning (line 2). 在开头(第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 [] . 这是因为你在这个变量的索引0处声明了一个数组,由于[] ,它被告知是一个数组。 You therefore try to place an array in your array, making it multidimensional. 因此,您尝试在数组中放置一个数组,使其成为多维数组。

You cannot simply echo an array. 你不能简单地echo一个数组。 echo can only output strings . echo只能输出字符串 echo 'foo' is simple, it's outputting a string. echo 'foo'很简单,它输出一个字符串。 What is echo supposed to do exactly in the case of echo array('foo' => 'bar') ? 什么是echo应该在的情况下,做的正是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" . 为了让echo在这里输出任何东西,PHP会将array('foo' => 'bar')转换为字符串,该字符串始终是字符串"Array" And because PHP knows this is probably not what you want, it notifies you about it. 而且因为PHP知道这可能不是你想要的,它会通知你。

The problem is you're trying to treat an array like a string. 问题是你正在尝试将数组视为字符串。 Fix that. 修复它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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