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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T20:07:19+00:00 2026-06-16T20:07:19+00:00

I have query automatic, i using code igniter, Structure table organization have field id

  • 0

I have query automatic, i using code igniter,
Structure table organization have field id and name.

 $get_organization = $this->db->query("SELECT id,name FROM organization")->result_array();

if i print_r variable $get_organization as below:

    Array (
         [0] => Array
          ( 
             [id] => 1
             [name] => orgone
          )

         [1] => Array
          (
             [id] => 2
             [name] => orgtwo
          )
   )

And for i get sum automatic i write code as below:

 foreach($get_organization as $bit){
     $id=$bit['id'];
     $name=preg_replace("/[^a-zA-Z0-9\-]/", "", $bit['name']);
     $getcabang[] = "SUM(CASE WHEN org_id = '$id' THEN total END) as $name";     
 }

 $cabang = implode(',',$getcabang);

And in table are looking to search the results is laporan with field id, total, tanggal_laporan, org_id.

 $query = $this->db->query("SELECT id, $cabang, tanggal_laporan FROM laporan GROUP BY tanggal_laporan")->result_array();

And then if i print_r variable $query i have result as below:

Array (
     [0] => Array
      ( 
         [orgone] => 218000
         [orgtwo] => 112000
         [tanggal_laporan] => 2012-11-04
      )

     [1] => Array
      (
         [orgone] => 198000
         [orgtwo] => 411000
         [tanggal_laporan] => 2012-11-05
      )

     [2] => Array
      (
         [orgone] => 513000
         [orgtwo] => 147000 
         [tanggal_laporan] => 2012-11-06
      )
 )

So my question, i want to result automatic as below:

$array_transform = array(
   [orgone] => "218000, 198000, 411000",
   [orgtwo] => "112000, 411000, 147000",
   [tanggal_laporan] => "2012-11-04, 2012-11-05, 2012-11-06",
);

And when it comes one more row of the table organization for example one row with name=orgthree, so result i want as:

$array_transform = array(
   [orgone] => "218000, 198000, 411000",
   [orgtwo] => "112000, 411000, 147000",
   [orgthree] => "0, 0, 0",
   [tanggal_laporan] => "2012-11-04, 2012-11-05, 2012-11-06",
);

Thanks.

  • 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-06-16T20:07:20+00:00Added an answer on June 16, 2026 at 8:07 pm

    Updated answer after the question was changed. This will process the fields into the structure you want withhout having to hard code the field names.

    Demo

    $array_transform = array();
    
    foreach($array_from_mysql as $row)
    {
        foreach($row as $field => $value)
        {
            if(isset($array_transform[$field]))
            {
                $array_transform[$field][] = $value;
            }
            else
            {
                $array_transform[$field] = array($value);
            }
        }
    }
    
    print_r($array_transform);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a query like this: select empno,name from emp where job = 'CLERK'
I have query like this : SELECT EXTRACT(MONTH FROM d.mydate) AS synmonth, SUM(apcp) AS
I have query to show the table like this: but I want to PIVOT
Have this query: SELECT HOUR( DATE ) AS hr, COUNT( * ) AS cnt
I have a query that successfully grabs the unique products from my products table
I have query with two MtM relations: $em = $this->getEntityManager(); $qb = $em->createQueryBuilder(); $qb
I have query table copyQuery which has Soil Condition has one of the columns,
I am using in C# MYsql .I have query that works if I run
I have the following code in my application: using (var database = new Database())
I have been using a basic caching system on my site based on this

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.