1

I have an array $csvRows like the following. I want to export that array as CSV.

Array
(
    [0] => SingleModule Object
        (
            [module:SingleModule:private] => Module Object
                (
                    [name:Module:private] => SimpleXMLElement Object
                        (
                            [0] => some string
                        )

                    [position:Module:private] => SimpleXMLElement Object
                        (
                            [0] => 1000
                        )

                    [config:Module:private] => SimpleXMLElement Object
                        (
                        )

                )

            [moduleMeta:SingleModule:private] => ModuleMeta Object
                (
                    [description:ModuleMeta:private] => description text
                    [featureOrUseCase:ModuleMeta:private] => feature or usecase text
                    [inputParam:ModuleMeta:private] => input Parameter text
                    [changedParam:ModuleMeta:private] => changed Parameter text
                    [newOutputParam:ModuleMeta:private] => new Output Param text
                    [commentOrNote:ModuleMeta:private] => comments Or Note text
                )

        )

    [1] => SingleModule Object
         (etc...)

I have already tried the solution, which I found in this link. Here is my code sample:

$fileName = "../../data/modules.csv";
$output = fopen($fileName, 'w+');
foreach ( $csvRows as $file ) {
    $result = [ ];
    array_walk_recursive($file, function ($item) use (&$result) {
        $result [] = $item;
    });
    fputcsv($output, $result);
}
fclose($output);

Then I get the following error:

PHP Recoverable fatal error:  Object of class Module could not be converted to string ...in line where fputcsv is.

I have temporarily solved the problem by manually creating a straightforward array like the following, which I found in this link:

 $csvRows = array(
        array( 'row_1_col_1', 'row_1_col_2', 'row_1_col_3' ),
        array( 'row_2_col_1', 'row_2_col_2', 'row_2_col_3' ),
        array( 'row_3_col_1', 'row_3_col_2', 'row_3_col_3' ),
    );

But I want to export my actual $csvRows array of objects as CSV. I am a newbie programmer, so any suggestion and help would highly appreciated.

1
  • Hi, I have tried your suggestion like the following: $str = json_encode($csvRows); $csvRows = json_decode($str, true); $fp = fopen("../../data/modules.csv", 'w+'); foreach ( $csvRows as $column ) { fputcsv($fp, $column); } fclose($fp); but I only have an empty csv file. Any idea? Commented Sep 25, 2017 at 14:14

1 Answer 1

1

Try the following solution:

$str = json_encode($csvRows);
$csvRows = json_decode($str, true);
foreach ($csvRows as $file) {
    $result = [];
    array_walk_recursive($file, function($item) use (&$result) {
        $result[] = $item;
    });
    fputcsv($output, $result);
}

Just convert all those object to associative array. And then add the array to CSV. Use JsonSerializable interface on SingleModule class and configure the following method:

class SingleModule implements JsonSerializable {
//YOUR CODE
    public function jsonSerialize() {
        return get_object_vars($this);
    }
}
Sign up to request clarification or add additional context in comments.

9 Comments

Hi, I have tried your suggestion like the following: $str = json_encode($csvRows); $csvRows = json_decode($str, true); $output = fopen("../../data/modules.csv", 'w+'); foreach ( $csvRows as $file ) { $result = [ ]; array_walk_recursive($file, function ($item) use (&$result) { $result [] = $item; }); fputcsv($output, $result); } but I only have an empty csv file. Any idea?
@IqbalNazir it might be due to every property of the objects being private. you can't access it outside the class.
yes, you are right. It's working when I have changed access modifier to public. Thanks for that. But I want to keep them private, and also export them as CSV. Any idea?
@IqbalNazir for that have a look at ReflectionClass
@IqbalNazir I have provided a solution for this in my answer.
|

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.