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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T21:34:37+00:00 2026-06-10T21:34:37+00:00

This may be a stupid question, so please go easy on me if so.

  • 0

This may be a stupid question, so please go easy on me if so.

I have a range of queries (MySQL) returning multidimensional arrays.

I then use these array items to populate variables in a string in a foreach loop.

I then need this string to populate a javascript graph so the format of the data wants to be perfect. this idea works with 1 multidimensional array.

However. To do multiple graphs (e.g comparison line graph) I need to express all the data in the same row.

So what I need to be able to do is if possible merge row to row of the array, rather than add it to the end.

Ill show you my working:

foreach ($graph_month as $month) :
        $first .= ' { year: "'.$month['month'].'",';
        endforeach;

        foreach ($graph_data1 as $data) :
        $second1 .= ' "'.$data['title'].'": '.$data['totalValue'].' ';
        endforeach;

        foreach ($graph_data2 as $data) :
        $second2 .= ' "'.$data['title'].'": '.$data['totalValue'].' ';
        endforeach;

        foreach ($graph_data3 as $data) :
        $second3 .= ' "'.$data['title'].'": '.$data['totalValue'].' ';
        endforeach;

        foreach ($graph_data4 as $data) :
        $second4 .= ' "'.$data['title'].'": '.$data['totalValue'].' ';
        endforeach;

        foreach ($graph_data5 as $data) :
        $second5 .= ' "'.$data['title'].'": '.$data['totalValue'].' ';
        endforeach;

So each of the foreach’s is populating a little section of the javascript required, however I need to be able to concatenate all these rows on the right of each other.

e.g

a foreach that can produce:

$first.$second1.$second2.$second3.$second4.$second5

Is this possible, would it be possible to add [i] and [i++] to each variable.

  • 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-10T21:34:38+00:00Added an answer on June 10, 2026 at 9:34 pm

    To start with, you could eliminate the extra foreach lines by generating variables like this:

    foreach ($graph_month as $month):
    
        for ($i = 1; $i <=5; $i++) {
            //$graph_data$i becomes $graph_data1, then $graph_data2 etc.
            foreach ($graph_data$i as $data) :
                $variable .= ' "'.$data['title'].'": '.$data['totalValue'].' ';
            endforeach;
        }
    
    // and so on
    endforeach;
    

    The outcome of this script is, as you requested, something of this shape:

    {year: month, title: value title: value

    We can tweak it a little like this:

    foreach ($graph_month as $month):
    
        $variable .= ' { year: "'.$month['month'].'",';
    
        for ($i = 1; $i <=5; $i++) {
            //$graph_data$i becomes $graph_data1, then $graph_data2 etc.
            foreach ($graph_data$i as $data) :
                $variable .= ' "'.$data['title'].'": '.$data['totalValue'].' ';
                if ($i != 5) {
                    $variable .= ", ";
                }
            endforeach;
        }
    
        $variable .= 
    
    // and so on
    endforeach;
    

    and it becomes:

    { year: month, 'title': 'value', 'title': 'value', ... }

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

Sidebar

Related Questions

This may be a stupid question but I have a code with the following
This may be a stupid question, but I have to ask! I have the
This may seem like a basic/stupid/obviously-answered question, but I wanted to check: why use
I know this may be a stupid question to ask but I have really
This question may seem like a novice, and perhaps 'stupid' question but please bear
First question on SOF, please be gentle if this may be a stupid question.
This may be stupid question. I have app with a bunch of URLs stored
This may be a stupid question but ... If I use JQuery .load(); etc
This may be a stupid question, but I still have it . Does having
I'm new to ASP.NET MVC so this may be a stupid question. I have

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.