0

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] => [email protected] 
                              [4] => it4249 
                              [5] => sha256:1000: 
                             ) 
              ) 
[1] => Array ( 
              [0] => Array ( 
                              [0] => fseconf 
                              [1] => test2 
                              [2] => 3 
                              [3] => [email protected] 
                              [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){
}
10
  • 2
    It works fine with me, so there's something else wrong. Commented Feb 9, 2014 at 7:47
  • 1
    Code works fine for me. Commented Feb 9, 2014 at 7:47
  • Oh strange - I've added the rest of my code in that loop in case there's something else going on? Commented Feb 9, 2014 at 7:53
  • 1
    What does $sth show? Also the same values or diffrent ones? And you can do $Row[0][4] without the quotes. Commented Feb 9, 2014 at 8:07
  • How would I echo that? Commented Feb 9, 2014 at 8:15

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Yep putting this snippet directly before my Foreach loop gives me the exact array output I have above that I expect. Within my Foreach loop I'm still just echoing the same value twice though.
Maybe, and it's just a maybe, print_r($excelData) causes a problem. If you remove that? or make it print_r($excelData,true)?
0

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

Demo

4 Comments

Use break then to stop executing last foreach after proper value found.
if ($k==4) works then there's no need to loop further last foreach. if($k==4) { echo $v.'<br/>'; break; }
Oh you are right. Added the break; Gave your creds in the code.
Supporting the theory that the error is not where I thought it was - this also doesn't work for me it also prints the first value twice
0

I have run your code . It works. There may be some problem with your array()

check demo

Check demo

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.