PHP Output to CSV from serialized data

Robert Granlund
Robert Granlund used Ask the Experts™
on
I have a database that has serialized data in it.  I need to export it to a CSV via a one click button.  Where do I start?  I'm not really sure where to start to ask the right question.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Scott FellDeveloper & EE Moderator
Fellow 2018
Most Valuable Expert 2013

Commented:
Based on your questions topics, should we assume you want to have a button that you can click.
<button class="download_button">Download</button>

Open in new window

Then use JS/Ajax to detect the click
$(function(){
   $('.download_button').on('click',function(){
        // Do Something 
    });
});

Open in new window


and access a php page that outputs the data
https://api.jquery.com/jQuery.ajax/
$(function(){
   $('.download_button').on('click',function(){
       $.ajax({
            method: "POST",
             url: "some.php",
             data: { name: "John", location: "Boston" }
             })
             .done(function( msg ) {
             alert( "Data Saved: " + msg );
  });
    });
});

Open in new window

On your remote php page you can use http://php.net/manual/en/function.fputcsv.php

Author

Commented:
what would the some.php look like when it queries a table that is serialized and outputs a CSV file?
Scott FellDeveloper & EE Moderator
Fellow 2018
Most Valuable Expert 2013

Commented:
That depends on what you mean by serialized.   If the data in a field looks something like "foo:bar,bat:baz" you can explode the text string http://php.net/manual/en/function.explode.php to create an array then use http://php.net/manual/en/function.fputcsv.php to output your csv.
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Author

Commented:
How would I un-serialize and run a for-each for this table that has one field that is serialized?  I think if I can un-serialize the one field and then run a for each statement, I believe I can output to a CSV.
query.JPG
Developer & EE Moderator
Fellow 2018
Most Valuable Expert 2013
Commented:
<?php

$data = 'a:1:"text1";b:2:"text2";c:3:"text3"';
$outer = explode(";",$data);
//print_r($outer);
foreach($outer as $inner){
    $x = explode(":",$inner);
    print_r($x);
    echo "<br>";
}

?>

Open in new window

Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
In PHP, you use the serialize() and unserialize() functions. So something like this:

$data = 'a:2:{s:9:"firstname";s:5:"chris";s:8:"lastname";s:7:"stanyon";}';

$newData = unserialize($data);

Open in new window

That will convert your serialized string back into a PHP variable that you can use. In the example above it will give you an array which you can use to write out your CSV. Depending on your data, you may also get back an object, a string, a bool etc.

Author

Commented:
I need to just grab the ii_email:

Array ( [data] => a:86:{s:9:"ii_seller";s:0:"";s:2:"id";s:3:"117";s:12:"ii_firstname";s:13:"Evander Fogle";s:10:"ii_address";s:59:" 5671 STREETAtlanta, GA 30342";s:8:"ii_email";s:26:"MY-EMAIL.COM";s:8:"ii_phone";s:12:"404-444-4444";s:10:"ii_pi_name";a:1:{i:0;s:0:"";}s:11:"ii_pi_phone";a:1:{i:0;s:0:"";}s:11:"ii_pi_email";a:1:{i:0;s:0:"";}s:11:"ii_pi_name2";s:0:"";s:12:"ii_pi_phone2";s:0:"";s:12:"ii_pi_email2";s:0:"";s:11:"ii_pi_name3";s:0:"";s:12:"ii_pi_phone3";s:0:"";s:12:"ii_pi_email3";s:0:"";s:15:"oi_hospitallist";a:4:{i:0;s:38:"Total Joint Replacement Prehab / Rehab";i:1;s:30:"Sports Medicine Prehab / Rehab";i:3;s:38:"Non-Surgical Orthopedic Prehab / Rehab";i:5;s:5:"Other";}
Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
Just posted a response to your other question specifically regarding grabbing a part of the array

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial