Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6900083
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:30:08+00:00 2026-05-27T07:30:08+00:00

I am trying to export data into an analysis but I’m doing something wrong.

  • 0

I am trying to export data into an analysis but I’m doing something wrong.

Basically I have a report that has between 1 and 9 subjects.
I am writing this export-tool from scratch, the data is from a legacy system.

The analysis should be exported as CSV, with columns like:

'report_id';'subject0';'subject1'…all the way to 'subject9'

I tried foreach-ing over an array of subjects of a report, and keeping count:

$iterator=0;
foreach ($subjects as $subject)
{
  $this->analysis [$report_id] ->subject${iterator} = (string)$subject;
  $iterator++;
}

Update
Because I may not use variables on the right side of the arrow operator, I’ve rewritten it as a very un-DRY switch:

$iterator = 0;
foreach ($categories as $subject)
  {
    switch ($iterator)
    {
    case 0:
      $this->analysis [$report_id] ->subject0 =  $subject;
      break;
    case 1:
      $this->analysis [$report_id] ->subject1 =  $subject;
      break;
    case 2:
      $this->analysis [$report_id] ->subject2 =  $subject;
      break;
    // I have 6 more cases, just to fill all 9 subjects, snipped for brevity

    default:
      //some errorhandling //snipped for brevity
      break 2;
    } 
    $iterator++;
  } 

$this->analysis [$report_id] is an object that stores strings as follows:
$this->analysis [$report_id] ->subject0 = "foo"
The $report_id is a result of an earlier loop.
$this->analysis is an array filled with (report)objects that are included in the analysis.

I hope that anyone can find the flaw and point me in the direction of DRY-ing up my implementation. Bonus points for the philosophy behind it, so that I’m not only helped with this problem, but have a better understanding of the oop-php world.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T07:30:08+00:00Added an answer on May 27, 2026 at 7:30 am

    I have no idea where the $report_id come from …
    Usually, I will do this :-

    $iterator = $i = 0;
    foreach ($categories as $subject)
    {
      $var = "subject{$i}";
      $this->analysis [$report_id]->$var =  $subject;
      ++$i;
      if ($i >= 10)
      {
        // what is your error?
      }
    }
    

    make use of variable to represent variable variable
    docs :- http://php.net/manual/en/language.variables.variable.php

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to export data from a web app into excel, but there is
I'm trying to export data from SQL Server into CSV format. I have a
In my application I am trying to export the data from the <h:DataTable> into
I'm trying to export a Crystal Report to an HTML file, but when I
I have been trying to export a mysql table with INTO OUTFILE to CSV
I am trying to pull data from a table I have imported into my
I am trying to export biometric data from an analysis using the ROCR package.
I'm trying to export data from an Excel spreadsheet into a fairly complex relational
I'm trying to export data from R into one Excel file (into different sheets):
I've been trying to export the content data of my ListView control into an

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.