0

I am reading excel data and storing it in php multi dimensional array. I would like to print multi dimensional array list values one by one as given in expected out put...

Php Code

   $sheetData = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true);
$i=0;
 foreach($sheetData as $rec)
{
    for($j=0;$j<4;$j=$j+1)
    {
     echo $rec[$i][$j];
    }
    $i=$i+1;
}

Actual Out put:

nothing displayed

var_dump($sheetData) shows as follows

array(4) { [1]=> array(4) { ["A"]=> string(10) "First Name" ["B"]=> string(9) "Last Name" ["C"]=> string(11) "Nationality" ["D"]=> string(6) "Gender" } [2]=> array(4) { ["A"]=> string(5) "AGNI" ["B"]=> string(6) "namera" ["C"]=> string(5) "india" ["D"]=> string(1) "M" } [3]=> array(4) { ["A"]=> string(4) "QUEEN" ["B"]=> string(5) "fefar" ["C"]=> string(5) "india" ["D"]=> string(1) "M" } [4]=> array(4) { ["A"]=> string(7) "MASTER" ["B"]=> string(5) "patel" ["C"]=> string(5) "India" ["D"]=> string(1) "M" } }

Expected output:

----------------
First Name
Last Name
Nationality
Gender
-----------------
AGNI
NAMERA
INDIA
M
------------------
...and so on...
2
  • 3
    Can you var_dump($sheetData); and paste here? Commented Feb 9, 2014 at 7:19
  • @user007: array(4) { [1]=> array(4) { ["A"]=> string(10) "First Name" ["B"]=> string(9) "Last Name" ["C"]=> string(11) "Nationality" ["D"]=> string(6) "Gender" } [2]=> array(4) { ["A"]=> string(5) "AGNI" ["B"]=> string(6) "namera" ["C"]=> string(5) "india" ["D"]=> string(1) "M" } [3]=> array(4) { ["A"]=> string(4) "QUEEN" ["B"]=> string(5) "fefar" ["C"]=> string(5) "india" ["D"]=> string(1) "M" } [4]=> array(4) { ["A"]=> string(7) "MASTER" ["B"]=> string(5) "patel" ["C"]=> string(5) "India" ["D"]=> string(1) "M" } } Commented Feb 9, 2014 at 7:25

2 Answers 2

2

You have two issues here... the first is that you are trying to access indexes on $rec as if it is $sheetData.

The line foreach($sheetData as $rec) means "loop through the array, and create a variable $rec each time which references the the current item in array $sheetData. So when you try to call index 0, 1, 2, etc. on $rec, they don't exist, which leads me to the second point...

In your for loop, you are trying to access items via their index as a numeric, when they are indexed by a string (A, B, C, D, etc.) For example...

<?php

$numeric = array('foo', 'bar');
$strings = array('foo' => 'bar');

In the first example, $numeric[0] will return foo, while $numeric[1] will return bar. In the second example, there is only one item in the array, with index foo. It would be accessed by $strings['foo']. Trying to access $strings[0] would return a notice because the index does not exist.

You should be doing this like so:

<?php

print "----------" . PHP_EOL;
foreach($sheetData as $rec)
{
    foreach($rec as $part)
    {
        print $part . PHP_EOL;
    }
    print "----------" . PHP_EOL;
}

You should probably read up on arrays. Also, if you got no output when running your script, you should probably set your error reporting level to E_ALL in your PHP configuration during development.

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

1 Comment

Thanks.. what is the meaning of null and true in "toArray(null,true,true,true)"
1

Try:

echo "------------"."<br>";
foreach($sheetData as $ind)
{
    foreach($ind as $index=>$val){
        echo $val."<br>";
    }
    echo "------------"."<br>";
}

2 Comments

@logan It's worth noting that in @user007's example, the foreach($ind as $index=>$val) line basically means "For every item in the array $ind, create a variable containing the index $index containing the item's index, and a variable $val containing the item's value.
Thank you guys... what is the meaning of null and true in "toArray(null,true,true,true)" in my question ?

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.