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
  • Is the expected end result JSON? Commented Sep 6, 2012 at 14:16

5 Answers 5

2

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', ... }

Sign up to request clarification or add additional context in comments.

Comments

1

You can put same variable:

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

foreach ($graph_data1 as $data) {
    $variable .= ' "'.$data['title'].'": '.$data['totalValue'].' ';
}

foreach ($graph_data2 as $data) {
    $variable .= ' "'.$data['title'].'": '.$data['totalValue'].' ';
}

// and so on

they will concatenate on every loop.

Comments

0

You can use the for loop like this...

<?php 

for($i=0;$i<count($graph_month);$i++)
{
    $all_values1 = $graph_data1[$i]['title'] . $graph_data1[$i]['totalValue'];
    $all_values2 = $graph_data2[$i]['title'] . $graph_data2[$i]['totalValue'];
    $all_values3 = $graph_data3[$i]['title'] . $graph_data3[$i]['totalValue'];
    $all_values4 = $graph_data4[$i]['title'] . $graph_data4[$i]['totalValue'];
    $all_values5 = $graph_data5[$i]['title'] . $graph_data5[$i]['totalValue'];
}

?>

No need to run too may for loop. It will take some extra time to execute...

Try like this....

Comments

0

Use Empty String

$string = '';

Then use this variable with every for loop

foreach($array1 as $value){
$string.= $value['some_data'];
}

foreach($array2 as $value){
$string.= $value['some_data'];
}

Use this $string as final result

Comments

0

Let PHP worry about the JSON formatting just build the object correctly.

<?php

$tojson = array(
     0 => array(),
     1 => array(),
     2 => array(),
     3 => array(),
     4 => array(),
     5 => array()
);

foreach($graph_month as $month)
    $tojson[0]['year'] = $month['month'];

foreach($graph_data1 as $data)
    $tojson[1][$data['title']] = $data['totalValue'];

foreach($graph_data2 as $data)
    $tojson[2][$data['title']] = $data['totalValue'];

foreach($graph_data3 as $data)
    $tojson[3][$data['title']] = $data['totalValue'];

foreach($graph_data4 as $data)
    $tojson[4][$data['title']] = $data['totalValue'];

foreach($graph_data5 as $data)
    $tojson[5][$data['title']] = $data['totalValue'];

$json = json_encode($tojson);

?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.