Jump to content

making arrays in a foreach loop


Sarah G

Recommended Posts

Hi,

 

I've got some data coming out of a CMS that I need to wrangle into an array for the Google Chart API. The data consists of a set of questions, multiple-choice answers for those questions, and then the number of results each answer has received. The questions come from an array ($questions), as do the answers/result counts ($row_data).

 

I've been able to get all of these pieces together and print them out right, like:

 

foreach ($questions as $question) {
  print $question['data'];    // questions 
  foreach ($row_data as $key => $value) {
    print $value[0];    // answers
    print $value[1];    // result counts
  }
}

 

But printing them out doesn't do anything very useful. To make these into charts, I need to get each question's answers and result counts onto another array that looks like this:

 

$chart_data = array(
  'answer choice 1' => 'result count',
  'answer choice 2' => 'result count'
);

 

Can someone explain or show how I'd make the $chart_data arrays out of the answer/result data? And how do I match up each $chart_data array with the question it does with?

 

Thanks!

Link to comment
Share on other sites

EDIT: Your code makes no sense. You do a foreach loop on $questions to create a $question variable, but then you do a foreach loop on $row_data. Where does $row_data come from?

 

Well, wherever it comes from you would do something like this

$chart_data = array();

    foreach ($row_data as $key => $value)
    {
        $chart_data[$value[0]] =  $value[1];
    }

Link to comment
Share on other sites

Thanks for your reply! I wish my code made sense, too. Sorry for the unclear question - I was trying to keep it simpler to make it easier to answer, but I just made it more confusing. What you posted helped, though. I didn't know it was possible to populate an array like how you did it there. That's really good to know.

 

The $row_data array came from a bigger array called $data, which I've since realized is the one I need to use since it's got the answer choices split up by question, and using $row_data doesn't give me a way to match up answer choices with the questions they relate to.

 

The $questions array looks like this: http://pastebin.com/esmUyj1m

The $data array looks like this: http://pastebin.com/CzdCXhCL

 

I'm trying to create an array for each question that holds it's answer choices as the keys and the result counts for those answers as the values.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.