Jim
Jim

Reputation: 1315

show an array inside of an array in a csv file using php

I have created an array using

    $processed[$y] = array('source' => $source,
            'check total' => number_format($checkTotal, 2, '.', ''),//($rows['total'], 2, '.', ''),
            'check number' => $num,
            'table' => $invTable,
            'skus' => $skuArray,
            'check amount' => number_format($amount, 2, '.', '')
            );
$y++;

My $skuArray is an array that contains all of the sku's that are associated with a specific check number. I am attempting to have it displayed like:

    source  check total check number  table        skus     check amount
    MNC       152.32       649       inv_temp    10198547   152.32
                                                 10195874

so it will list all of the sku's attached to a specific check nuimber before it lists the next item.
Here is my function to convert $processed to a csv file:

    function to_csv( $array ) {
 $csv = "";

 if (count($array) == 0) return "No Processed checks found";

 ## Grab the first element to build the header
 $arr = array_pop( $array );
 $temp = array();
 foreach( $arr as $key => $data ) {
   $temp[] = $key;
 }
 $csv = implode( ',', $temp ) . "\r\n";

 ## Add the data from the first element
 $csv .= to_csv_line( $arr );

 ## Add the data for the rest
 foreach( $array as $arr ) {
   $csv .= to_csv_line( $arr );
 }

 return $csv;
    }

    function to_csv_line( $array ) {
 $temp = array();
 foreach( $array as $elt ) {
   $temp[] = '"' . addslashes( $elt ) . '"';
 }

 $string = implode( ',', $temp ) . "\r\n";

 return $string;
    }

How can I accomplish this? I have tried using array('skus=>$skuArray), but it just gave me "Array" in the results.

UPDATE: Here is what the array looks like when I do a var_dump($skuArray) array(1075) { [0]=> string(8) "10182997" [1]=> string(8) "10190313" [2]=> string(8) "10190314" [3]=> string(8) "10190315" etc.

Upvotes: 0

Views: 434

Answers (2)

Axel
Axel

Reputation: 10772

I've provided a solution that is untested, so use at your own discretion.

I've done my best to explain everything through comments in the code.

  1. Remove the first sku value from the sku array, assign it to the first line.

  2. Add additional skus to a temporary array based on the line keys.

  3. Check for temporary sku array, and create the additional lines from it.

Your final to_csv function will look something like this:

function to_csv( $array ) {
    $csv = "";

    if (count($array) == 0) return "No Processed checks found";

    ## Grab the first element to build the header
    $arr = $array[0];
    $temp = array();
    foreach( $arr as $key => $data ) {
        $temp[] = $key;
    }
    $csv = implode( ',', $temp ) . "\r\n";

    ## Process each line
    foreach( $array as $arr ) {

        ## Check for multiple sku values.  Create a temporary array for them to add them after this line.
        if(isset($arr['skus']) && is_array($arr['skus']))
        {
            //Remove the first value (since we only need it for the actual line item)
            $sku_value = $arr['skus'][0];
            unset($arr['skus'][0]);

            //Create temporary lines for each sku
            $temp_sku_arrays = array();
            foreach($arr['skus'] as $sku)
            {
                $sku_array = array();
                foreach($arr as $key => $value)
                {
                    //Set only the sku key with a value.
                    $sku_array[$key] = ($key == 'skus' ? $sku : '');
                }
                $temp_sku_arrays[] = $sku_array;
            }

            //Set the first line to the first sku value.
            $arr['skus'] = $sku_value;          
        }  

        $csv .= to_csv_line( $arr );

        //Check for additional sku lines, then add them
        if(isset($temp_sku_arrays) && is_array($temp_sku_arrays))
        {
            foreach($temp_sku_arrays as $sku_array)
            {   
                $csv .= to_csv_line( $sku_array );
            }
            unset($temp_sku_arrays);
        }
    }

    return $csv;
}

Upvotes: 1

hek2mgl
hek2mgl

Reputation: 158040

I think CSV is not well suited for what you are about to do. I would use json or xml. However, you could choose a separator different from the csv sepator to represent an array, Like this:

foo,bar1;bar2;bar3,...

what would represent the following record:

$record = array (
   'foo',
   array ('bar1', 'bar2', 'bar3')
);

Upvotes: 0

Related Questions