Jump to content

Complex (multidimensional array) CSV Creation


luminous

Recommended Posts

I've been stuck on this for about a week so I was wondering if I can get some help from you guys!

 

Basically I'm exporting an array of values to a CSV file, within that array are two other arrays containing data I need. What I need to work out is how I can create a CSV with headers for these values, then with the nested arrays also print their values into the same rows as the first array. Example:

 

$List = array (

'Product ID' => '10',

'Customer Address' => '123 Fake Street',

array(

        array('Product Name'=>'Product1', 'Product Price'=>'10.00', 'Product Reference'=>'HGJEN'),

        array('Product Name'=>'Product2', 'Product Price'=>'5.00', 'Product Reference'=>'HGJTN'),

        array('Product Name'=>'Product3', 'Product Price'=>'10.00', 'Product Reference'=>'HGJNN'),

),

array(

        array('Product Customisation Name'=>'Additional Info', 'Customisation Value'=>'Things are great.'),

        array('Product Customisation Name'=>'Image Upload', 'Customisation Value'=>'Logo.jpg'),

   

),

'Telephone Number'=>'999',

 

);

 

To be exported to something looking like this:

 

Product ID

Customer Address

Product Name

Product Price

Product Reference

Product Cus Name

Cus Value

Telephone Number

10

123 Fake Street

Product2

5.00

HGJTN

Additional Info

Things are great

999

 

10

123 Fake Street

Product1

10.00

HGJEN

Image Upload

Logo.jpg

999

10

123 Fake Street

Product3

10.00

HGJNN

[/td]

[td]

999

 

If anyone could help me out in anyway I would be so appreciative, I'm sure this ones going to end up killing me! :P

Link to comment
Share on other sites

It's not so much complex as bad structure. I would instead focus on how I could obtain an array with each row specified, most CSV parsers do this.

 

You're definitely going to have to recreate the array if you want to have it in any kind of format where you can manage the rows efficiently.

 

In fact, hold on. I'll create the function (I have some time on my hands). However, it's up to you to amend the loop I give you to produce a table. (I hope you know how to do that).

Link to comment
Share on other sites

I've created the new function for you. T get a better constructed array, simply use this:

 

$newList = parseCsvArray($List);

 

which will produce:

 



Array
(
    [0] => Array
        (
            [Product ID] => 10
            [Customer Address] => 123 Fake Street
            [Product Name] => Product1
            [Product Price] => 10.00
            [Product Reference] => HGJEN
            [Product Customisation Name] => Additional Info
            [Customisation Value] => Things are great.
        )

    [1] => Array
        (
            [Product ID] => 10
            [Customer Address] => 123 Fake Street
            [Product Name] => Product2
            [Product Price] => 5.00
            [Product Reference] => HGJTN
            [Product Customisation Name] => Image Upload
            [Customisation Value] => Logo.jpg
        )

    [2] => Array
        (
            [Product ID] => 10
            [Customer Address] => 123 Fake Street
            [Product Name] => Product3
            [Product Price] => 10.00
            [Product Reference] => HGJNN
            [Product Customisation Name] => 
            [Customisation Value] => 
        )

)

 

..and you can then loop this very easily to produce your rows:

 

foreach($newList as $row){
  print_r($row);
}

 

..if you don't know how to use that array, I'd suggest reading up on how to output tables using arrays.

 

The only bad thing about this array is that it is very specific and rigid. If I used an object or spent more time, I could have developed it into a far more dynamic parser. For sake of time, I opted for a simple parser.

 

..and, in it's terrible glory, here it is:

 

function parseCsvArray($array){
  
  $productArray = array();
  $productRow = array();

  // determine how big array is
  $count = count($array[0]);
  
  for($n = 0; $n < $count; $n++){
    
    $productRow['Product ID'] = $array['Product ID'];
    $productRow['Customer Address'] = $array['Customer Address'];
    $productRow['Product Name'] = $array[0][$n]['Product Name'];
    $productRow['Product Price'] = $array[0][$n]['Product Price'];
    $productRow['Product Reference'] = $array[0][$n]['Product Reference'];
    $productRow['Product Customisation Name'] = isset($array[1][$n]['Product Customisation Name']) ? $array[1][$n]['Product Customisation Name'] : '' ;
    $productRow['Customisation Value'] = isset($array[1][$n]['Customisation Value']) ? $array[1][$n]['Customisation Value'] : '' ;
    
    $productArray[] = $productRow;
  }

  return $productArray;
}

 

Hope that helps!

Link to comment
Share on other sites

You my friend are an absolute saint! This has really helped me out, I've created an object to handle everything much more neatly but I'd been staring at this thing for so long my brain had turned to moosh, I just needed someone to push me in the right direction! Thank you again, your time is very much appreciated  :'(

:D

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.