简体   繁体   中英

Parse a multi dimentional array in PHP

I have an array

[1] => Array (
[1] => Report
)
[2] => Array (
[1] => Merchant Name
[2] => Merchant Id
[3] => Merchant Ref No
[4] => Trans Date
[5] =>  Ref No
[6] => Trans Type
[7] => Trans Amt
[8] => Trans Fee
[9] => Serv Tax
[10] => Total Fee
[11] => Amt Settled
[12] => Neft Ref.No
[13] => Settlmnt Date
[14] => Funds Settld Date
)
[3] => Array (
[1] => yy Private Limited
[2] => ESSIZMM000
[3] => zWidItN33452373TcH
[4] => 2014-12-02 10:27:47.82
[5] => 265135490490229682
[6] => SALES
[7] => 249
[8] => 2.241
[9] => 0.277
[10] => 2.518
[11] => 246.482
[12] => ABB SL : 00012
[13] => 04-12-2014
[14] => 04-12-2014
)

This is an array I got after parsing an excel.The first array is the header of the excel where the column names are there and the second array onwards is the real data comes.Can some one help me how to parse out all the 11th index of the array except the first array?Here the 11th index is an amount , but in the first array ,is the header 'Amt Settled' which I don't need.

I tried this

$cells = $worksheet['cells'];

foreach ($cells as $key => $value)
{
    echo $value['11'];
}

where $cells is the array which is printed above.But when I'm using this i'm getting

Amt Settled246.482

How can I skip that first array ,ie the header of the excel.Is there some better suggestion for this other than my code?Please help

Try this -

$i = 0;
foreach ($cells as $key => $value)
{
    if ($i > 2) {
        echo $value['11'];
    }
    $i++;
}

Try this :

<?php
$your_array = your array goes here;
array_shift($your_array);
print_r($your_array);

//here you do your foreach

?>

Ref: http://php.net/manual/en/function.array-shift.php

Edit: I think in your case you have to use array_shift 2 times to remove this also : [1] => Array ( [1] => Report )

If so try this :

<?php
$your_array = your array goes here;
array_shift($your_array);
array_shift($your_array);
print_r($your_array);

//here you do your foreach

?>

Assuming that the cell data starts at ['cells'][3] you can slice the array from index 3 down:

$cells = array_slice($worksheet['cells'], 3, NULL, TRUE);

foreach ($cells as $key => $value)
{
    echo $value['11'];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM