与数组相关的问题

时间:2011-03-11 05:37:56

标签: php

我正在从excel文件中读取此格式数据。然后我将其转换为数组。 现在我想进行计算。我尝试了很多次但没有成功。感谢

example
L*M    100*200
L*H    100*300

 0           1      2
 UserData       
 Length 100      L
 Width  200       W
 Height 300      H
 AdminData      
 L*W    Result  
 L*H    Result  
 W*H    Result  
 Total  TotalRest   

使用print_r()

的数组结果
 Array
 (
   [0] => Array
    (
        [0] => UserData
        [1] => 
        [2] => 
    )

[1] => Array
    (
        [0] => Length
        [1] => 100
        [2] => L
    )

[2] => Array
    (
        [0] => Width
        [1] => 200
        [2] => W
    )

[3] => Array
    (
        [0] => Height
        [1] => 300
        [2] => H
    )

[4] => Array
    (
        [0] => AdminData
        [1] => 
        [2] => 
    )

[5] => Array
    (
        [0] => L*M
        [1] => 
        [2] => 
    )

[6] => Array
    (
        [0] => L*H
        [1] => 
        [2] => 
    )

[7] => Array
    (
        [0] => W*H
        [1] => 
        [2] => 
    )

[8] => Array
    (
        [0] => Total
        [1] => 
        [2] => 
    )

数组函数

$arrResult = array();

 /////////////Open the csv file which contains data//////////////

$handle = fopen("data.csv", "r");
if( $handle ) {
///////Put the data in an array///////////
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {      
$arrResult[] = $data;
}
fclose($handle);
}

1 个答案:

答案 0 :(得分:0)

$var  = array(

    array('UserData'),

    array('Length', 100, 'L'),

    array('Width', 200, 'W'),

    array('Height', 300, 'H'),

    array('AdminData'),

    array('L*W'),

    array('L*H'),

    array('W*H'),

    array('Total')
    );

echo "<pre>"; 

print_r($var);

echo "</pre>";

$var[5][1] = $var[1][1]*$var[2][1];

$var[6][1] = $var[1][1]*$var[3][1];

$var[7][1] = $var[3][1]*$var[2][1];

echo "<pre>"; 

print_r($var);

echo "</pre>";