Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php count()返回错误的值_Php_Arrays - Fatal编程技术网

Php count()返回错误的值

Php count()返回错误的值,php,arrays,Php,Arrays,我正在使用以下代码: $row_arr=$_POST['row_driver']; print_r($row_arr); 返回: 数组([0]=>d1[1]=>d2[2]=>d3[3]=>d5) 但是 返回给我的值是 一, 有什么原因吗 在这里,row_驱动程序是一个数组,它是使用HTML表单的隐藏元素属性通过表单从上一个PHP页面接收的。而且 foreach($row_arr as $driver) { //code here } 正在返回: 警告:中为foreach()提供的参数无效 第

我正在使用以下代码:

$row_arr=$_POST['row_driver'];
print_r($row_arr);
返回:

数组([0]=>d1[1]=>d2[2]=>d3[3]=>d5)

但是

返回给我的值是

一,

有什么原因吗

在这里,row_驱动程序是一个数组,它是使用HTML表单的隐藏元素属性通过表单从上一个PHP页面接收的。而且

foreach($row_arr as $driver)
{
//code here
}
正在返回:

警告:中为foreach()提供的参数无效 第36行的D:\XAMPP\htdocs\Carpool\booking\u feed.php


问题在于隐藏字段

foreach ($rows as $value){
<input type="hidden" name="row_driver[]" value="<?php echo $value; ?>">
}
foreach($value为行){

您可以将计数值存储在某个变量中:

$row_arr=Array('d1','d2','d3','d4');
print_r($row_arr);

$count = count($row_arr);
echo 'Your Count is:- '.$count;

您面临的问题是
$\u POST['row\u driver']
不是数组


如果您有一个隐藏的HTML输入:

<input type="hidden" name="row_driver" value="<?php print_r($rows); ?>">
,因此,
count()
函数的结果是1

这也解释了您面临的第二个问题,即
foreach()
,函数需要一个数组,但您提供的是一个字符串

解决方案是对隐藏的HTML输入使用foreach循环,如下所示:

<?php foreach($rows as $row_driver){?>
    <input type="hidden" name="row_driver[]" value="<?php echo $row_driver; ?>"/>
<?php }?>

:

表情

The expression to be printed. return

If you would like to capture the output of print_r(), use the return parameter. When this parameter is set to TRUE, print_r() will
返回信息而不是打印它

返回值

如果给定字符串、整数或浮点,则会打印值本身。 如果给定一个数组,值将以显示 键和元素。类似的符号用于对象

当return参数为TRUE时,此函数将返回一个字符串。 否则,返回值为TRUE

print\r()
可以用作特殊的打印方法,以显示数组中的所有值以及关联数组(对此更有用)

关联数组:

是使用指定给它们的命名键的数组



如果您使用
echo
,您必须使用数组索引打印它。例如
$row\u arr[0]
,或者如果您使用关联数组而不是索引,则会使用键。它可能是字符串。

此项的可能副本看起来像索引数组如果您有一个隐藏的HTML输入,则不会
$\u POST['row\u driver']
be a string?这还可以解释您面临的第二个问题,即
foreach()
@JustBaron我不确定它是字符串还是数组,但我已经使用
初始化了值。但是它的替代解决方案是什么?但我的数组不是这样。在设置值时,我可以看到隐藏字段的值是如何设置的吗?请删除打印,然后检查并签入控制台行驱动程序的值是多少printed@JustBaron通过他的建议解决了我的问题。它返回相同的值。
<?php foreach($rows as $row_driver){?>
    <input type="hidden" name="row_driver[]" value="<?php echo $row_driver; ?>"/>
<?php }?>
The expression to be printed. return

If you would like to capture the output of print_r(), use the return parameter. When this parameter is set to TRUE, print_r() will