迭代到元素数组然后乘以任意数字?

时间:2017-10-02 06:43:58

标签: php arrays

如果我有这样的数组?

[
0 => [
    'sheet' => 'SUB-ARYA'
    'int' => 1
    'supplier_name' => 'TON DONG A CORPORATION'
    'contract_no' => 'D606D17LS091'
    'coil_no' => 'DNA07X2B419011701A'
    'size' => '0.42MM X 101MM X COIL'
    'net' => 4.65
    'nama_file' => 'STO SUB JKT 08082017.xlsx'
]
1 => [
    'sheet' => 'SUB-ARYA'
    'int' => 2
    'supplier_name' => 'NAM KIM STEEL JOINT STOCK COMPANY'
    'contract_no' => 'N014P17LS044'
    'coil_no' => '03NKXTL170400423/30'
    'size' => '0.70MM X 151.8MM X COIL'
    'net' => 4.944
    'nama_file' => 'STO SUB JKT 08082017.xlsx'
]
2 => [
    'sheet' => 'SUB-ARYA'
    'int' => 3
    'supplier_name' => 'TON DONG A CORPORATION'
    'contract_no' => 'D000G57LS597'
    'coil_no' => 'DNA07X2B511051718A'
    'size' => '0.42MM X 101MM X COIL'
    'net' => 4.74
    'nama_file' => 'STO SUB JKT 08082017.xlsx'
]
3 => [
    'sheet' => 'SUB-ARYA'
    'int' => 4
    'supplier_name' => 'TON DONG A CORPORATION'
    'contract_no' => 'D000G37LS377'
    'coil_no' => 'DNA10X1B422051704A'
    'size' => '0.43MM X 101MM X COIL'
    'net' => 4.65
    'nama_file' => 'STO SUB JKT 08082017.xlsx'
]
4 => [
    'sheet' => 'SUB-ARYA'
    'int' => 5
    'supplier_name' => 'TON DONG A CORPORATION'
    'contract_no' => 'D000G37LS378'
    'coil_no' => 'DNA10X2A421051713A'
    'size' => '0.43MM X 93.5MM X COIL'
    'net' => 4.72
    'nama_file' => 'STO SUB JKT 08082017.xlsx'
]
] 

请参阅元素net中的值。 有:

'net' => 4.65 // in 0 index
'net' => 4.944 // in 1 index
'net' => 4.74 // in 2 index
'net' => 4.65 // in 3 index
'net' => 4.72 // in 4 index

我需要将它们操作为整数形式,

'net' => 4650 // in 0 index
'net' => 4944 // in 1 index
'net' => 4740 // in 2 index
'net' => 4650 // in 3 index
'net' => 4720 // in 4 index

如何以简单的方式创建呢? 请指教

3 个答案:

答案 0 :(得分:3)

你可以这样做是一个foreach循环:

foreach($yourArray as $i=>$v) {
   $yourArray[$i]['net'] *= 1000;
}

答案 1 :(得分:3)

只需使用array_walk(简单地保持愚蠢):

array_walk($yourArray, function (&$item, $key) {
    $item['net'] *= 1000;
});

答案 2 :(得分:2)

您可以使用array_map执行此操作。例如:

function modify($el){
    // this trick will return whole element, instead of only modified one
    $t = $el;
    $t['net'] = $t['net'] * 1000;
    return $t;
}
$result = array_map("modify", $yourArray);