2012-02-10 84 views
0

我想通過foreach只遍歷PHP中的特定子數組。例如數組:通過特定的關聯數組在PHP中循環

$testData = array( 
    $test1=array( 
     'testname'=>'Test This', 
     'testaction'=>'create user', 
     $testData = array(
      'item'=>'value', 
      'foo'=>'bar', 
      'xyz'=>'value' 
     ), 
     $anotherArray = array() 
    ), 
    $test2=array( 
     'testname'=>'Test That', 
     'testaction'=>'get user', 
     $testData = array(
      'item'=>'value', 
      'foo'=>'bar', 
      'xyz'=>'value' 
     ), 
     $anotherArray = array() 
    ) 
); 

,現在我要去通過每個測試和設置基礎上的名字和行動一些邏輯,但隨後需要進行數據的多次測試。不知道如何獲得$ test1的$ testData而不是$ test1的$ anotherArray數據。我有以下但它不起作用:

foreach($testData as $test => $section){ 
    foreach($section['testData'] as $field => $value){ 
     \\code 
    } 
} 

任何幫助表示讚賞!謝謝!

+0

'$ testData1'是不是正確構建陣列。你應該收到一個錯誤。使用Laverdure的答案,您可以使用'$ testData ['test1'] ['testData']'來訪問您的數據。 – Josh 2012-02-10 20:53:22

+0

你用'$ testData1'指的是什麼@Josh – jheep 2012-02-10 20:57:31

+0

對不起,這是一個錯字。我只是想'$ testData'。 – Josh 2012-02-10 20:58:24

回答

1

試試這個:

$testData = array( 
'test1'=>array( 
    'testname'=>'Test This', 
    'testaction'=>'create user', 
    'testData' => array(
     'item'=>'value', 
     'foo'=>'bar', 
     'xyz'=>'value' 
    ), 
    'anotherArray' => array() 
), 
'test2'=>array( 
    'testname'=>'Test That', 
    'testaction'=>'get user', 
    'testData' => array(
     'item'=>'value', 
     'foo'=>'bar', 
     'xyz'=>'value' 
    ), 
    'anotherArray' => array() 
) 
); 
+0

謝謝!它現在似乎在工作。 – jheep 2012-02-10 21:03:19