dsgft1486 2014-02-09 07:36
浏览 48

访问数组中数组索引的值

I have an array of arrays called $excelData. print_r($excelData) returns the following:

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:

$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?

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条回答 默认 最新

  • doubihuai8468 2014-02-09 07:41
    关注

    Nest down 3 times.

    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
    

    <kbd>Demo</kbd>

    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图