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 7616817
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T02:58:59+00:00 2026-05-31T02:58:59+00:00

how to format sql server rows using php that look like this: id company

  • 0

how to format sql server rows using php that look like this:

id     company         value      monthyear
1      companyone      30         january2012
2      companytwo      20         february2012
3      companyone      10         february2012

into this:

monthyear: ['january2012', 'february2012']

and this:

company: 'companyone', value: [30, 10]
company: 'companytwo', value: [0, 20]

each instance of a month from the db is combined into one instance.

company one, which has two rows, is combined into one instance where each value is lined up in order of the month. company two, which only has one instance, has it’s value defined as 0 where it has no instance in a month.

the farthest i’ve gotten is are two two dimensional array with array_merge_recursive and some conditional statements but then my head goes into knots.

  • 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-31T02:59:00+00:00Added an answer on May 31, 2026 at 2:59 am
    SELECT
        company, 
        GROUP_CONCAT(value SEPARATOR ',') AS value, 
        GROUP_CONCAT(monthyear SEPARATOR ',') AS monthyear
    FROM
        yourTable
    GROUP BY
        company
    

    Some Reference for GROUP_CONCAT.

    PHP solution:

    Select the to be grouped attribute sorted (company). Loop over them and open a new group every time you encounter a different value for company. As long as the current row has the same row as the previous, add value and monthyear to the current company.

    You could do this even without sorting:

    while($row = mysql_fetch_assoc($resource))
    {
        $values[$row["country"]][] = $row["value"];
        $monthyear[$row["country"]][] = $row["monthyear"];
    }
    

    Some output example

    foreach ($values as $country => $valuesOneCountry)
    {
        // each country
        var_dump($country);
    
        foreach ($valuesOneCountry as $i => $value)
        {
            // value, monthyear for each original row
    
            var_dump($value, $monthyear[$country][$i]);
        }
    }
    

    Elegant way with OOP:

    class Tuple
    {
        public $country, $values, $monthyears;
    
        public function __construct($country, $values = array(), $monthyears = array())
        {
            $this->country = $country;
            $this->values = $value;
            $this->monthyears = $monthyears;
        }
    }
    
    $tuples = array();
    while($row = mysql_fetch_assoc($resource))
    {
        if (!isset($tuples[$row["country"]]))
             $tuples[$row["country"]] = new Tuple($row["country"]);
    
        // save reference for easy access        
        $tuple = $tuples[$row["country"]];
    
        // or some method like $tuple->addValue($row["value"]);
        $tuple->values[] = $row["value"];
        $tuple->monthyears[] = $row["monthyear"];
    }
    
    var_dump($tuples);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using SQL Server 2000 My Query. SELECT (Format(IIf(CLng(OutTime) > 180000, CDate('18:00:00'), CDate(Format(OutTime, '00:00:00'))) -
How to see this format hh:mm from hh:mm:ss in SQL Server 2008 query ?
I have a php script that backup my table in .sql format and I
Good afternoon to all, I have this scenario: I am using SQL Server 'BulkInsert'
I have a column in a SQL Server table that has the following rows:
Using SQL Server and VB6 Table1 Date Time 20090801 060000 20090802 162000 Date Format:
Using SQL Server, have a name value pair table. Each row is basically userid,
We are converting our application from using Sql Server Express (let's call this version
I would like to know which format SQL Server saves datetimes, GMT or UMT?
How do I retrieve a date from SQL Server in YYYY-MM-DD format? I need

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.