2017-07-19 52 views
0

我試圖從第n個(這種情況下,第三個內部)數組中的數組插入以下數據到數據庫中。 $bigDataArray包含另外兩個數組,但我們感興趣的是其中的最後一個(第三個)數組。該$requiredArray返回這麼多的記錄,並有很多領域。我希望只選擇幾個字段並將與它們關聯的所有值插入到數據庫中。還應該注意的是,由於我們剛剛以這種形式提供了數據,因此從012數據庫中獲取這些數據的操作不是而是。任何援助將不勝感激。Foreach在第n個數組的數據庫中插入多個值

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 \交流\ r \響應對象 ( [無法識別的:保護] =>數組 ( )

[_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] => 

)`

+0

你能否給我們提供'$ bigDataArray'的'print_r'? – MinistryofChaps

+0

@MinistryofChaps我們無法訪問'$ bigDataArray'中的所有數據,但僅當我們訪問所提供的方法時才能訪問。我應該爲'$ requiredArray'提供部分嗎? – BekiTheMe

+0

@BikiTheMe你可以不用'print_r($ bigDataArray-> getAll())'來顯示它嗎?如果不是,$ requiredArray會很好。另外,如果你想獲得最後一個$ bigDataArray數組,你可以這樣做:'$ lastArray = $ bigDataArray [count($ bigDataArray) - 1]' – MinistryofChaps

回答

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>";