内爆数组值
implode an array value
我有一个多select属性的颜色。
$color = $_product->getAttributeText('color');
$output = implode(',', $color);
echo $output;
$color给出一个数组值。如果颜色属性有多个值,比如 1.blue 和 2.green,它会打印 blue,green 但是当 $color 只存在一个属性时(比如 blue),它不会打印任何东西。
这是内爆的正常行为吗?数组中必须存在多个值?如果不是,那么我如何打印这些单个现值?
您可以使用 is_array()
.
$color = $_product->getAttributeText('color');
if (is_array($color)) {
$output = implode(',', $color);
} else {
$output = $color;
}
echo $output;
我要在这里冒险,但我会猜测如果有一个值 returned 它是一个字符串;字符串对于 implode
是无效输入,将引发 PHP Warning
.
在这种情况下,implode
将 return 一个 null
值,这可以解释为什么您看不到任何打印。
因此请确保在所有情况下都将数组传递给 implode
。
编辑
如果您在 开发 环境中看不到调试信息,那么您应该考虑设置 error_reporting()
以帮助您调试代码。一种简单的方法是将以下几行添加到脚本的顶部:
<?php
error_reporting(E_ALL|E_STRICT);
ini_set('display_errors', true);
// your code...
请注意,出于安全原因,您不应在生产环境中启用这些设置。
Hello Dear,
$color = 'blue';
$output = implode(',', $color);
echo $output;
It will give you a warning.
Warning: Invalid arguments passed.
But it works fine with array
$color = array('blue');
$output = implode(',', $color);
echo $output;
Check the return value stored in `$color` and then go ahead.
Thanks.
我有一个多select属性的颜色。
$color = $_product->getAttributeText('color');
$output = implode(',', $color);
echo $output;
$color给出一个数组值。如果颜色属性有多个值,比如 1.blue 和 2.green,它会打印 blue,green 但是当 $color 只存在一个属性时(比如 blue),它不会打印任何东西。
这是内爆的正常行为吗?数组中必须存在多个值?如果不是,那么我如何打印这些单个现值?
您可以使用 is_array()
.
$color = $_product->getAttributeText('color');
if (is_array($color)) {
$output = implode(',', $color);
} else {
$output = $color;
}
echo $output;
我要在这里冒险,但我会猜测如果有一个值 returned 它是一个字符串;字符串对于 implode
是无效输入,将引发 PHP Warning
.
在这种情况下,implode
将 return 一个 null
值,这可以解释为什么您看不到任何打印。
因此请确保在所有情况下都将数组传递给 implode
。
编辑
如果您在 开发 环境中看不到调试信息,那么您应该考虑设置 error_reporting()
以帮助您调试代码。一种简单的方法是将以下几行添加到脚本的顶部:
<?php
error_reporting(E_ALL|E_STRICT);
ini_set('display_errors', true);
// your code...
请注意,出于安全原因,您不应在生产环境中启用这些设置。
Hello Dear,
$color = 'blue';
$output = implode(',', $color);
echo $output;
It will give you a warning.
Warning: Invalid arguments passed.
But it works fine with array
$color = array('blue');
$output = implode(',', $color);
echo $output;
Check the return value stored in `$color` and then go ahead.
Thanks.