0

My googlefoo has failed me. Given this array:

   [0] => Array
        (
            [sku] => abc123
            [price] => 19.95
            [special_price] => 0
            [tier20] => 13.48
            [tier40] => 16.98
            [tier50] => 17.48

        )

    [1] => Array
        (
            [sku] => def456
            [price] => 129.98
            [special_price] => 79.98
            [tier50] => 123.48
            [tier100] => 116.98
            [tier250] => 110.48
        )

The first 3 key->values will be the same between all elements (~2500 or so), but the last 3 there could be several dozen of total.

I need to generate a CSV file from this, where each sub-array (product) is a line, and each key gets a column? I specifically need each [tierX] to have a unique column.

2
  • So what is the expected output format? CSV can be any format. Also what have you tried? Just saying you tried googling is not enough Commented Feb 24, 2020 at 17:26
  • My problem is that that the output by default does not give the tiers s a unique column. I need the output like this: sku, price, special_price, tier20, tier40, tier50, tier100, tier200, etc Commented Feb 24, 2020 at 18:01

1 Answer 1

2

I first find all possible tiers then loop the array again to build a table array.
There are comments in code to explain most of the code.

$arr = array (
  0 => 
  array (
    'sku' => 'abc123',
    'price' => '19.95',
    'special_price' => '0',
    'tier20' => '13.48',
    'tier40' => '16.98',
    'tier50' => '17.48',
  ),
  1 => 
  array (
    'sku' => 'def456',
    'price' => '129.98',
    'special_price' => '79.98',
    'tier50' => '123.48',
    'tier100' => '116.98',
    'tier250' => '110.48',
  ),
);

//Find all possible tiers 
$tier =[];
foreach($arr as $sub){
    $tier = array_merge($tier, preg_grep("/(tier.*)/", array_keys($sub)));
}
//Unique tiers
$tier = array_unique($tier);

//Count tiers
$columns = count($tier);

//Header of CSV with tiers
$header = 'sku, price, special_price, ' . implode(', ', $tier);

foreach($arr as $row => $sub){
    foreach($sub as $key => $item){
        // Add items to array (only first three are start)
        $table[$row][$key] = $item;

        // After first three items add empty placeholders with associative keys to tiers array
        if(count($table[$row]) == 3){
            $table[$row] = array_merge($table[$row], array_combine($tier, array_fill(3, $columns, 0)));
        }
    }
}

//For debug purpose, output the CSV header then each row.
echo $header . "\n";
foreach($table as $row){
    echo implode(', ', $row) . "\n";
}

//var_dump($tier, $header, $table);

https://3v4l.org/lJnXL

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! This did exactly what I was looking for.

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.