Sam Jackson
Sam Jackson

Reputation: 628

Traversing a multi-dimensional array

I'm interacting with the Magento API and after calling:

$result = $soap->call( $session_id, 'catalog_product.list' );

I get an array, full of arrays with information inside of them, I know this because after performing print_f on it I get the following result:

Array( [0] => Array( [product_id] => 2 [sku] => 401HCS [name] => Paul Penders Hydrating Control Serum (20g) [set] => 4 [type] => simple [category_ids] => 

Array ( [0] => 4 [1] => 15 [2] => 43 ) )

[1] => Array ( [product_id] => 3 [sku] => 400ICT [name] => Paul Penders Intensive Clarifying Therapy (ICT) [set] => 4 [type] => simple [category_ids]

Array ( [0] => 4 [1] => 11 [2] => 43 ) ) 

[2] => Array ( [product_id] => 4 [sku] => 402CFE [name] => Paul Penders Herbal Citrus Fruit Exfoliant (60ml) [set] => 4 [type] => simple [category_ids] => 

It is not indented obviously, i did that for easy reading, so my question is how would I go about traversing some kind of loop in order to go into every array and get the [product_id] and the other elements? Thanks in advance!

Upvotes: 2

Views: 2548

Answers (1)

André
André

Reputation: 12737

It's tagged 'java' but it clearly is php. You can traverse it like this:

foreach ($result as $id => $data) {
   foreach ($data as $key => $value) {
      switch ($key) {
         case 'product_id':
            // do things
            break;
         case 'sku':
            // do things
            break;
         // (...)

Upvotes: 5

Related Questions