jheep
jheep

Reputation: 134

Loop Through Specific Associative Array In PHP

I am trying to loop through only a specific sub array in PHP with foreach. Example array:

$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()
     )
);

And now I am going to go through each test and set some logic based on the name and action, but then need to do several tests on the data. Not sure how to only get $test1's $testData and not $test1's $anotherArray data. I have the following but it doesn't work:

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

Any help is appreciated! Thanks!

Upvotes: 0

Views: 429

Answers (1)

Luc Laverdure
Luc Laverdure

Reputation: 1480

Try this instead:

$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()
 )
);

Upvotes: 1

Related Questions