• Resolved Steve Borsch

    (@sborsch)


    Am trying to figure out how to display one of two array values from a custom dropdown form field called “Sex” to an export to CSV. The values are “Male” and “Female” but this is what is exported in to the CSV:

    a:1:{i:0;s:4:”Male”;}
    a:1:{i:0;s:6:”Female”;}

    Yes, it is trivial to change these manually after exporting, but I want it to be simple for the client so they can just use a template in WP All Export Pro to run these exports periodically.

    I’m not a PHP coder so have tried to modify code snippets I’ve found online and have gone through at least 25 of them thus far (and have about 6 hours invested) and none work.

    Any ideas would sure be appreciated for using WP All Export Pro’s “Export the value returned by a PHP function” capability to export just the user’s selection of either “Male” or “Female” as values when running the export to CSV.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author WP All Import

    (@wpallimport)

    Hi @sborsch

    Sure, here’s an example snippet that you can use for this:

    function my_get_value( $data ) {
    	if ( ! empty( $data ) ) {
    		$data = maybe_unserialize( $data );
    		return reset( $data );
    	}
    }
    Thread Starter Steve Borsch

    (@sborsch)

    Really appreciate the code! But I’m wondering if manually editing user records made this not work?

    In about 75% of the user records we manually changed a:1:{i:0;s:4:”Male”;} to “Male” and a:1:{i:0;s:6:”Female”;} to “Female“. For the newest 25% of the user records, your code snippet did not return only “Male” and “Female” but instead it output the entire a:1:{i:0;s:4:”Male”;} and a:1:{i:0;s:6:”Female”;} again.

    The error is:

    Warning: reset() expects parameter 1 to be array

    string given in /home/myserverusername/public_html/wp-content/uploads/wpallexport/functions.php on line 5

    Any guidance or ideas?

    Plugin Author WP All Import

    (@wpallimport)

    Hi @sborsch

    To fix the manually adjusted data, you should add a check in the code after the maybe_unserialize() function to see if it’s an array – if it is, use the reset function, else just return the $data variable.

    As for the code not returning the proper value for the data that hasn’t been adjusted, please make sure you’ve enabled “Export the value returned by a PHP function” and typed in the function name: http://www.wpallimport.com/tour/export-developer-friendly/

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Pulling value from an array with PHP to export to CSV’ is closed to new replies.