从多维数值数组中提取数据

时间:2014-02-02 17:43:26

标签: php arrays extract

我有2维数组,里面填充了表中的数据。

我想提取每个值并将其用于计算并将结果保存到新数组

例如:

$array[0][0] + 5 =  $result0

$array[0][1] + 35 = $result1

$array[0][2] + 55 = $result2

$array[0][3] + 15 = $result3

$array[0][4] + 75 = $result4

然后对主阵列中的整个下一个阵列和所有其他阵列重复相同的计算。

Array
(
    [0] => Array
        (
            [0] => 1
            [ID] => 1
            [1] => 0
            [name] => 0
            [2] => 0
            [date] => 0
            [3] => 5
            [number] => 5
            [4] => 0
            [size] => 0
        )

    [1] => Array
        (
            [0] => 2
            [ID] => 2
            [1] => 0
            [name] => 0
            [2] => 0
            [date] => 0
            [3] => 3
            [number] => 3
            [4] => 0
            [size] => 0
        )

    [2] => Array
        (
            [0] => 3
            [ID] => 3
            [1] => 0
            [name] => 0
            [2] => 0
            [date] => 0
            [3] => 7
            [number] => 7
            [4] => 0
            [size] => 0
        )

)

1 个答案:

答案 0 :(得分:1)

要从二维数组中提取每个元素,请使用如下所示的foreach循环并声明一个新数组,并在更改后在数组中添加更新的值。

$cars = array
      (
      array(1,"id"=>100,"name"=>96),
      array(0,"id"=>60,"name"=>59),
      array(5,"id"=>110,"name"=>100)
      );



$result= array();


  foreach ($cars as $v1) { 
    foreach ($v1 as $v2) { 
        $result[]= $v2+5; 
    } 
} 
  print_r($result);