Sarah G Posted July 11, 2011 Share Posted July 11, 2011 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! Quote Link to comment Share on other sites More sharing options...
Psycho Posted July 11, 2011 Share Posted July 11, 2011 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]; } Quote Link to comment Share on other sites More sharing options...
Sarah G Posted July 12, 2011 Author Share Posted July 12, 2011 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.