Wilf Wilf - 19 days ago 5
MySQL Question

How to make a response data in array for Morris

I want to create a data array from a return value of mysql. The raw data from mysql is :

$chart=Array (
[bd] => Array (
[Eid] => 59100
[Mona] => 156050
[Nai] => 27750
[Nana] => 90680
[Pinya] => 9400
[Pok] => 43900
[Tunk] => 48600
[VEE] => 26800
)
[cp] => Array (
[Eid] => 23650
[Mona] => 86760
[Nai] => 54160
[Nana] => 125300
[Pinya] => 63960
[Pok] => 59800
[Tunk] => 111260
[VEE] => 125460
)
)


I want the final result comes in this format:

{
hotel: 'BD',
Eid:59100,
Mona:156050,
Nai:27750,
Nana:90680,
Pinya:9400,
Pok:43900,
Tunk:48600,
VEE:26800,
}...


So, I write :

foreach($chart as $hotel=>$rep){
echo "{hotel:$hotel, $rep},";
}


And the result is:

{hotel:bd, Array},{hotel:cp, Array},{hotel:cs, Array},{hotel:km, Array}{hotel:nk, Array},


It is similar to what I want but how can I extract the
Array
into the format shown above?

Answer

This Code Works .

$finalArray = array();
$i = 0;
foreach($chart as $hotel=>$rep) {

    $finalArray[$i] = "{<br>hotel: $hotel,<br>";

    foreach($rep as $key=>$val) {
        $finalArray[$i] .= "$key:$val,<br>";
    }

    $finalArray[$i] .= "}<br>";     
    $i++;
}

$finalString = implode(",",$finalArray);
echo $finalString;