output php multi-dimensional array

I've tried to create a multi dimensional array (see below) but I'm having trouble outputting it.

Array ( [prods] => Array ( [0] => Array ( [0] => Polo X [1] => [2] => [3] => [4] => [5] => ) ) [sizes] => Array ( [0] => Array ( [0] => S [1] => [2] => [3] => [4] => [5] => ) ) [qtys] => Array ( [0] => Array ( [0] => 9 [1] => [2] => [3] => [4] => [5] => ) ) [totals] => Array ( [0] => Array ( [0] => 876 [1] => 876 [2] => 876 [3] => 876 [4] => 876 [5] => 876 ) ) )

I created it as follows  (where $errorProds is an array)
            $errorStocks = array(
            "prods"=>array($errorProds),
            "sizes"=>array($errorSizes),
            "qtys"=>array($errorQtys),
            "totals"=>array($errorStocks))      ;

I'd like to output it as a list
prods1, sizes1, qtys1, totals1
prods2, sizes2, qtys2, totals2
etc...

thanks
joomlaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Beverley PortlockCommented:
I find the creation of the "master array" a little odd. Each item in it is already an array and you are putting that inside another array , so to access the first element of "prods" you would need

$errorStocks ['prods'][0][1]

rather than

$errorStocks ['prods'][1]

If that is what you intended then the attached code will need modification to add the [0] array element throughout the code (replace $arr[$name] with $arr[$name][0]) because I have assumed that it is a misundertsanding. If not then let me know.

<?php


$errorProds  = array( 'ep1', 'ep2', 'ep3' );
$errorSizes  = array( 'es1', 'es2', 'es3' );
$errorQtys   = array( 'eq1', 'eq2', 'eq3' );
$errorStks   = array( 'st1', 'st2', 'st3', 'st4' );  // for testing, this array is too long


$errorStocks = array(
                 "prods"=> $errorProds,
                 "sizes"=> $errorSizes,
                 "qtys"=>$errorQtys,
                 "totals"=>$errorStks
               );
               

               



     /**
      * Output a structured multidimensional array
      * 
      * @param array $arr An array of subarrays
      */
     function outputArray( $arr ) {
          
          // Fetch the names of all the sub arrays
          //
          $keys = array_keys($arr);


                    
          // Determine the longest sub array (should all be the smae size)
          //
          $maxSize = 0;
          foreach( $keys as $name ) {
               $size = count( $arr[$name] );

               if ( $size > $maxSize )
                    $maxSize = $size;
          }

          
          // Output the values
          //
          if ( $maxSize > 0 ) {
               for ( $i=0; $i < $maxSize; $i++ ) {
                    $out = array();
                    
                    // Output a value, but check if this array is shorter than the longest
                    // known one and if so, add some padding
                    //
                    foreach( $keys as $name )                       
                         $out [] = ( isset( $arr[$name][$i]) ) ? $arr[$name][$i] : '';
                                        
                    echo implode(',', $out);
                    echo "<br/>";
               }
               
          }
     }
     
     
     
// Test
//
outputArray( $errorStocks );     

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
joomlaAuthor Commented:
Hi,
can you tell me how I could create the array better.

I have a function that generates four arrays or lists of data that I want to return to the calling routine.

I'm much rather take more time to pass the content from the function to the routine in the most efficient way

thanks
M
0
Beverley PortlockCommented:
"can you tell me how I could create the array better."

I am not sure what you mean by this. The method you are using is OK, I just found it a little odd that you had an "array of an array of arrays" rather than the simpler "array of arrays".

My own preference would be to create a small class just to hold the data and then build an array of class objects which I would then pass to the output function, but that is really a whole separate question and completely invalidates the answer to this question.

It is not that hard to do. I have an EE article that shows the outlines of it and if you want to post a sepearte question on it then I will show you how to approach it for this specific problem

http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_3212-Using-PHP-classes-to-group-data-in-memory.html
0
joomlaAuthor Commented:
thanks for the additional infomation
exactly what I need to get a handle on it
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Scripting Languages

From novice to tech pro — start learning today.