0

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.

0

2 Answers 2

1

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;
}
Sign up to request clarification or add additional context in comments.

12 Comments

thanks Axel, I tried what you have, and it is only returning the sku array for the last item in the $processed[$y] array 9 so it is the first item listed in the csv file. Any idea on how to fix this? thanks again for the help.
Correction, the sku is for the first item in the list, not the last.
Are there any additional lines added after the first line (blanks?).
just has "Array" where the sku should be.
Please be more descriptive. 1 line with Array in the row (no blank lines after?)
|
0

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')
);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.