简体   繁体   English

注意:数组到字符串转换PHP

[英]Notice: Array to string conversion PHP

I am loading a friends site to play with it and it gives me this: Notice: Array to string conversion error. 我正在加载一个朋友网站来玩它并且它给了我:注意:数组到字符串转换错误。 Any idea's how to fix this? 任何想法如何解决这一问题? (my friend is out of town) (我的朋友不在城里)

function href($filename) {
    if (@$_SERVER['HTTPS'] != 'on') {
        return HTTP_IMAGE . $filename;
    } else {
        return HTTPS_IMAGE . $filename;
    }   
}

the error points to the 3rd line on this code. 错误指向此代码的第3行。

You're mixing array and string in a return . 你在return混合数组和字符串。 Try the following: 请尝试以下方法:

  • specify an index to $filename (like $filename[0] ) 指定$filename的索引(如$filename[0]

or: 要么:

  • convert it into a string with for example implode() function (like implode(',', $filename); , which gives you an string with the values of the array $filename , segregated by , ). 将它转换为一个带有例如implode()函数的字符串(如implode(',', $filename);它为您提供一个字符串,其值为数组$filename ,隔离, )。

You are using a string concatenation operator with something that is not a string. 您正在使用字符串连接运算符与不是字符串的东西。 Namely, with an array. 即,使用数组。

Try this: 尝试这个:

function href($filename) {
var_dump($HTTP_IMAGE);
var_dump($filename);
    if (@$_SERVER['HTTPS'] != 'on') {
        return HTTP_IMAGE . $filename;
    } else {
        return HTTPS_IMAGE . $filename;
    }   
}

When you run this code it should display whatever is inside HTTPS_IMAGE and $filename. 当您运行此代码时,它应显示HTTPS_IMAGE和$ filename中的任何内容。

As I do not know what they contain, I will explain with the following sample code: 由于我不知道它们包含什么,我将使用以下示例代码进行解释:

$my_array[0] = 'Hello';
$my_array[1] = 'world';
$my_array[2] = '!';
var_dump( $my_array );

If you execute that code you will obtain the following: 如果您执行该代码,您将获得以下内容:

array(3) {
  [0]=>
  string(5) "Hello"
  [1]=>
  string(5) "world"
  [2]=>
  string(1) "!"
}

That is an array. 那是一个数组。 It is a collection of elements, which need to be accessed by index. 它是元素的集合,需要通过索引访问。 For instance, to access "world" you need to access it by its index, "1": 例如,要访问“world”,您需要通过索引“1”访问它:

echo $my_array[1];

Returning to your example, identify which variable is an array and access it by the appropriate index. 回到您的示例,确定哪个变量是一个数组并通过适当的索引访问它。 You will know which index is by inspecting the result of var_dump(). 通过检查var_dump()的结果,您将知道哪个索引。

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

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