Foreach从第n个数组中插入数据库中的多个值

时间:2017-07-19 13:50:35

标签: php

我试图从第n个(这种情况下是第3个内部)数组中的数组将以下数据插入数据库。 $bigDataArray包含两个以上的数组,但我们对其中的最后一个(第三个)数组感兴趣。 $requiredArray返回如此多的记录并且包含许多字段。我希望只选择几个字段并将与其关联的所有值插入数据库。还应该注意的是,从mysql数据库中获取此数据,因为我们刚刚提供了该表单中的数据。任何帮助将受到高度赞赏。

foreach ($bigDataArray->getAll() as $requiredArray) {
             foreach ($requiredArray as $cols => $records) { 
             // just pick the required data for analysis
                    if($cols == "xx1" || $cols == "xx2" || $cols == "xx4" || $cols == "xxn"){

                     // I want insert $cols as fields and $row as records in database,then just update values in xx2 if xx1 already exists

                    }
               }      
            }

print_r($requiredArray) `y \ ac \ r \ _Response对象 (     [无法识别:受保护] =>排列         (         )

[_type:y\ac\r\Response:private] => !re
[attributes:protected] => Array
    (
        [.id] => 3
        [name] => Kelvin
        [target] => b.b.bb.g
        [parent] => none
        [rpap] => 
        [rpip] => 8/8
        [defval] => default-small/default-small
        [l-at] => 512000/1000000
        [m-it] => 512000/2000000
        [b-l] => 0/0
        [b-ld] => 0/0
        [b-ti] => 0s/0s
        [ck-s] => 0.1/0.1
        [activity] => 1645827604/17691224739
        [tbs] => 0
        [pts] => 11900213/13011698
        [tkets] => 0
        [loss] => 98154/2662855
        [t-loss] => 0
        [avg-r] => 70064/2025768
        [p-rate] => 155/172
        [tpt-rate] => 0
        [qets] => 0/4
        [tets] => 0
        [qutes] => 0/5968
        [ttes] => 0
        [inv] => false
        [dyn] => true
        [dis] => false
    )

[_tag:_type:y\ac\r\Message:private] => 

)`

1 个答案:

答案 0 :(得分:0)

我会在这里给你一个答案:

  

$ bigDataArray包含两个以上的数组,但我们对其中的最后一个(第三个)数组感兴趣。

要访问最后一个数组,您可以使用以下命令:

$lastArray = $bigDataArray[count($bigDataArray) - 1];

这将创建一个变量,其最后一个数组为$bigDataArray,供您访问。

因此,如果您想要从最后一个数组中取出字段,您现在需要做的就是调用$lastArray['field']

如果我正确理解你的问题,这里有一段代码我在实践中表明我的观点。

<?php
$bigDataArray = array();

$array1 = array("field1" => "valueA1", "field2" => "valueA1", "field3" => "valueA1");
$array2 = array("field1" => "valueA2", "field2" => "valueA2", "field3" => "valueA2");
$array3 = array("field1" => "valueA3", "field2" => "valueA3", "field3" => "valueA3");

array_push($bigDataArray, $array1);
array_push($bigDataArray, $array2);
array_push($bigDataArray, $array3);

echo "bigDataArray:";

echo "<pre>";
print_r($bigDataArray);
echo "</pre>";

echo "lastArray:";
echo "<pre>";
$lastArray = $bigDataArray[count($bigDataArray) - 1];
print_r($lastArray);
echo "</pre>";