0

I have data that's setup the following way:

array(
      0 => array(
                  'data_id' => 0,
                  'elem_value' => 'phone',
                  'elem_prettyname' => 'Phone Number'
                 ),
      1 => array(
                  'data_id' => 0,
                  'elem_value' => 'email',
                  'elem_prettyname' => 'Email Address'
                 ),
      2 => array(
                  'data_id' => 1,
                  'elem_value' => 'phone',
                  'elem_prettyname' => 'Phone Number'
                 ),
      3 => array(
                  'data_id' => 1,
                  'elem_value' => 'email',
                  'elem_prettyname' => 'Email Address'
                 )
     )
)

I'm trying to turn that into a csv file that looks like:

"Data ID","Phone Number","Email Address"
0,"phone","email"
1,"phone","email"

But I can't figure it out for the life of me. I have tons of random arrays and they go nowhere.

I should mention that the elem_prettyname can have an infinite number of elements.. IE not just Phone Number and Email Address.

Can anyone point me in the right direction to get that formatted correctly?

Thanks in advance.

2
  • "infinite dimensions" means something completely different than "contains lots of different values for elem_perttyname". Try showing a real array and converting "roughly like" to "exactly like this" so people can give you specific answers. Commented May 17, 2012 at 23:06
  • Well, your answer was exactly what I needed. So thanks for guessing. Commented May 17, 2012 at 23:23

3 Answers 3

1

you need to convert your data to an array that looks like this: 0,"phone","email" first.

Something like this:

$lines = array();
foreach($inTheQuestion as $row) {
    if (empty($lines[$row['data_id']])) {
        $lines[$row['data_id']] = array($row['data_id']);
    }
    $lines[$row['data_id']][] = $row['elem_value'];
}
var_dump($lines);

If that's the format you want (which isn't clear from the question) - you then simply dump the data into a file with fputcsv or, simply, with fwrite.

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

Comments

1

I'm sure there's a more succinct way of doing this, but here's a bash. It works for me...

$array = array(
      0 => array(
                  'data_id' => 0,
                  'elem_value' => 'phone',
                  'elem_prettyname' => 'Phone Number'
                 ),
      1 => array(
                  'data_id' => 0,
                  'elem_value' => 'email',
                  'elem_prettyname' => 'Email Address'
                 ),
      2 => array(
                  'data_id' => 1,
                  'elem_value' => 'phone',
                  'elem_prettyname' => 'Phone Number'
                 ),
      3 => array(
                  'data_id' => 1,
                  'elem_value' => 'email',
                  'elem_prettyname' => 'Email Address'
                 )
     );

$string = '"Data ID","Phone Number","Email Address"' . "\n";
foreach($array as $r){
    $data_id[$r['data_id']][] = $r['elem_value'];   
}
foreach($data_id as $k => $d){
    $string .= $k . ",\"" . implode('","',$d) . "\"\n"; 
}
file_put_contents('my.csv',$string);

I'm assuming there's always phone, email - otherwise it'll get jumbled.

Comments

1

If you want to write csv file you can use fputcsv()

Do

fputcsv($fp, array_keys(reset($my_array)));

to ouptut first line with labels and

foreach($my_array as $item) {
  fputcsv($fp, array_values($item));
}

to output rest. $fp must be file handle of the file opened for writing. You can get it with fopen()

2 Comments

That won't work in the environment I'm in. Just tried it but to no avail :(
Sorry. I didn't notice your data is really bizzare in comparison to the csv file you want to get. I just focused on outputting the csv file.

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.