I have an array of arrays called $excelData. print_r($excelData) returns the following:
我有一个名为$ excelData的数组。 print_r($ excelData)返回以下内容:
Array (
[0] => Array (
[0] => Array (
[0] => name
[1] => test
[2] => 4
[3] => test@test.com
[4] => it4249
[5] => sha256:1000:
)
)
[1] => Array (
[0] => Array (
[0] => fseconf
[1] => test2
[2] => 3
[3] => example@test.com
[4] => ft9655
[5] => sha256:1000:
)
)
)
and I'm trying to print the 4th index in each case (i.e. it4249 and ft955) with the following code:
我正在尝试使用以下代码打印每种情况下的第四个索引(即it4249和ft955):
$query = "INSERT INTO tblTest (username, fname, surname, year, email) VALUES";
$qPart = array_fill(0, count($excelData), "(?, ?, ?, ?, ?)");
$query .= implode(",",$qPart);
$sth = $dbh->prepare($query);
$i = 1;
print_r($excelData);
echo "<br />";
echo "<br />";
Foreach($excelData As $Row){
echo "Username: ".$Row[0][4];
echo "<br />";
echo "<br />";
$sth->bindValue($i++, $Row[0][4]);
$sth->bindValue($i++, $Row[0][0]);
$sth->bindValue($i++, $Row[0][1]);
$sth->bindValue($i++, $Row[0][2]);
$sth->bindValue($i++, $Row[0][3]);
}
But it simply prints it4249 both times. Why does this not work and how do I get this right?
但它只是打印它42249。为什么这不起作用,我该如何做到这一点?
EDIT:
编辑:
Changing my loop to pass by reference as below solves my problem but I have no idea why - any explanations?
改变我的循环以通过引用传递如下解决了我的问题,但我不知道为什么 - 任何解释?
Foreach($excelData As &$Row){
}
3 个解决方案
#1
1
A little snippet to possibly find where it goes wrong. Place if before you define your query to see if it ouputs what you expect, and if it's ok after the query, and so on.
一小段可能找到它出错的地方。在定义查询之前放置,以查看它是否输出了您期望的内容,以及查询后是否正常,依此类推。
<?php
reset($excelData);
echo count($excelData)."<br>"; //should be 2
do{
echo 'KEY: '.key($excelData); //fist 0, then 1
$tmp=current($excelData);
echo '<pre>'.print_r($tmp,true).'</pre><br>';
}
while(next($excelData));
?>
It gives me the output you showed. If not, there's defenitely something wrong with your array.
它给了我你展示的输出。如果没有,你的阵列肯定存在问题。
#2
0
Nest down 3 times.
窝下3次。
foreach ($other as $arr)
{
foreach($arr as $arr1)
{
foreach($arr1 as $k=>$v)
{
if($k==4)
{
echo $v.'<br/>';
break; // as suggested by u_mulder
}
}
}
}
OUTPUT :
输出:
it4249
ft9655
演示
#3
0
I have run your code . It works. There may be some problem with your array()
我已经运行了你的代码。有用。你的数组可能有问题()
check demo
检查演示
检查演示
#1
1
A little snippet to possibly find where it goes wrong. Place if before you define your query to see if it ouputs what you expect, and if it's ok after the query, and so on.
一小段可能找到它出错的地方。在定义查询之前放置,以查看它是否输出了您期望的内容,以及查询后是否正常,依此类推。
<?php
reset($excelData);
echo count($excelData)."<br>"; //should be 2
do{
echo 'KEY: '.key($excelData); //fist 0, then 1
$tmp=current($excelData);
echo '<pre>'.print_r($tmp,true).'</pre><br>';
}
while(next($excelData));
?>
It gives me the output you showed. If not, there's defenitely something wrong with your array.
它给了我你展示的输出。如果没有,你的阵列肯定存在问题。
#2
0
Nest down 3 times.
窝下3次。
foreach ($other as $arr)
{
foreach($arr as $arr1)
{
foreach($arr1 as $k=>$v)
{
if($k==4)
{
echo $v.'<br/>';
break; // as suggested by u_mulder
}
}
}
}
OUTPUT :
输出:
it4249
ft9655
演示
#3
0
I have run your code . It works. There may be some problem with your array()
我已经运行了你的代码。有用。你的数组可能有问题()
check demo
检查演示
检查演示