Get details from php array

Dear All,

I am uploading an excel with 2 columns

Yes i can read excel file. sample attached.

Code

<?php
require 'vendor/autoload.php';

use PhpOffice\PhpSpreadsheet\IOFactory;

try {
    $spreadsheet = IOFactory::load('uom.xlsx');
    $sheet = $spreadsheet->getActiveSheet();

    // Get the highest row and column numbers referenced in the worksheet
    $highestRow = $sheet->getHighestRow(); // e.g. 10
    $highestColumn = $sheet->getHighestColumn(); // e.g 'F'
  
    
    // Iterate over each row in the worksheet in turn
    for ($row = 1; $row <= $highestRow; $row++) {
        $rowData = $sheet->rangeToArray('A' . $row . ':' . $highestColumn . $row, NULL, TRUE, FALSE);
        print_r($rowData)."<br>";
          
 
         
    }
} catch (Exception $e) {
    echo 'Error loading file: ', $e->getMessage();
}
?>

I am getting output (php script print_r($rowData)

Array ( [0] => Array ( [0] => Measure [1] => UPS Code ) )

Array ( [0] => Array ( [0] => Bag [1] => BG ) )

Array ( [0] => Array ( [0] => Barrel [1] => BA ) )

Array ( [0] => Array ( [0] => Bolt [1] => BT ) )

this is result of

print_r($rowData)."<br>"

so i tried

echo "0=".$rowData[0]." 1=".$rowData[1] to get individual cell
(Assuming $rowData is an array holding values of 2 cells)

I get (could not get indivudual cell values.
Pls help me how to get values of individual cells

pls help/ guide to get individual cell values

Output after above change (echo "0=".$rowData[0]" 1=".$rowData[1] to get individual cell)

Array ( [0] => Array ( [0] => Measure [1] => UPS Code ) )

Separate Array *** rowData[0] =Array rowData[1] << rowData
Array ( [0] => Array ( [0] => Bag [1] => BG ) )

Separate Array *** rowData[0] =Array rowData[1] << rowData
Array ( [0] => Array ( [0] => Barrel [1] => BA ) )

Separate Array *** rowData[0] =Array rowData[1] << rowData
Array ( [0] => Array ( [0] => Bolt [1] => BT ) )

Separate Array *** rowData[0] =Array rowData[1] << rowData
Array ( [0] => Array ( [0] => Box [1] => BOX ) )

Separate Array *** rowData[0] =Array rowData[1] << rowData
Array ( [0] => Array ( [0] => Bunch [1] => BH ) )

Separate Array *** rowData[0] =Array rowData[1] << rowData
Array ( [0] => Array ( [0] => Bundle [1] => BE ) )

If you change the print_r() line to -

echo '<pre>'; print_r($rowData); echo '</pre>';

so that the output is formatted, you should get the following -

Array (
	[0] => Array (
		[0] => Measure
		[1] => UPS Code
	)
)

Array (
	[0] => Array (
		[0] => Bag
		[1] => BG
	)
)

Array (
	[0] => Array (
		[0] => Barrel
		[1] => BA
	)
)

Array (
	[0] => Array (
		[0] => Bolt
		[1] => BT
	)
)

Each $rowData is an array, with a single [0] element, which is an array of the row’s data. You can either loop over the row’s data or access individual elements using $rowData[0], e.g. -

foreach($rowData[0] as $value)
{
    echo $value.'<br>';
}

or

echo $rowData[0][0].'<br>';
echo $rowData[0][1].'<br>';
Sponsor our Newsletter | Privacy Policy | Terms of Service