Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change Array Result PHP and MYSQL

Tags:

arrays

php

mysql

I need help to change array result from mysql data using php for creating some report.

This is array result that i get from query result mysql and php

Array
(
    [0] => Array
        (
            [AIRL] => GA 
            [TICK] => 39
            [JUM] => 103000000.00
        )

    [1] => Array
        (
            [AIRL] => JT 
            [TICK] => 31
            [JUM] => 42485000.00
        )

    [2] => Array
        (
            [AIRL] => SJY
            [TICK] => 3
            [JUM] => 3020000.00
        )

    [3] => Array
        (
            [AIRL] => KD 
            [TICK] => 3
            [JUM] => 3011000.00
        )

    [4] => Array
        (
            [AIRL] => QG 
            [TICK] => 1
            [JUM] => 2696000.00
        )

)

And the array result above need to changed to this array :

Array
(
    [0] => Array
        (
            [0] => AIRL
            [1] => TICK
            [2] => JUM
        )

    [1] => Array
        (
            [0] => GA
            [1] => 39
            [2] => 103000000
        )

    [2] => Array
        (
            [0] => JT
            [1] => 31
            [2] => 42485000
        )

    [3] => Array
        (
            [0] => SJY
            [1] => 3
            [2] => 3020000
        )

    [4] => Array
        (
            [0] => KD
            [1] => 3
            [2] => 3011000
        )

    [5] => Array
        (
            [0] => QG
            [1] => 1
            [2] => 2696000
        )

)

The code so far i've got like this :

<?php
/** Connection Database */
$myServer ="xxx";
$conn_inv = mysql_connect($myServer, 'xxx', 'xxx');
mysql_select_db('xxx');

/** Query Database */
$query = "SELECT     TOP 20 d.AIRL AS AIRL, COUNT(d.TICK) AS TICK, SUM(d.VALUE) AS JUM
FROM         TICKET d INNER JOIN
                      Invoice h ON h.INVNO = d.INVNO AND h.TYPE = d.TYPE
WHERE     (h.INVDATE >= '05/01/2015') AND (h.INVDATE <= '05/15/2015') AND (h.TYPE = 'TIX')
GROUP BY d.KD_AIRL
ORDER BY JUM DESC";  
$result = mysql_query($query);
$tes = array();
while($row=mysql_fetch_assoc($result)){
    array_push($tes, $row);
}

$data = array(
                array('AIRL','TICK','JUM'),
                array('GA',   39,       103000000),
                array('JT',   31,       42485000),
                array('SJY',  3,        3020000),
                array('KD',   3,        3011000),
                array('QG',   1,        2696000),
            );

// THIS IS ARRAY RESULT FROM QUERY DATABASE         
echo "<pre>";
print_r($tes);
echo "</pre>";
echo "<br/>";

// THIS IS ARRAY RESULT I WANTED
echo "<pre>";
print_r($data);
echo "</pre>";
?>
like image 241
kroseva Avatar asked Feb 10 '23 19:02

kroseva


2 Answers

Changed like this:

$tes = array();
while($row=mysql_fetch_array($result)){
    array_push($tes, $row);
}
like image 68
Ramki Avatar answered Feb 12 '23 10:02

Ramki


Use this:

$array = array(array('AIRL'=>'GA','TICK'=>39,'JUM'=>103000000.00),array('AIRL'=>'JT','TICK'=>31,'JUM'=>42485000.00));
$new = array();
foreach($array as $k=>$a){
    if($k == 0){
        $new[] = array_keys($a);
        $new[] = array_values($a);
    }else{
        $new[] = array_values($a);
    }
}
var_dump($new);

Output:

array (size=3)
  0 => 
    array (size=3)
      0 => string 'AIRL' (length=4)
      1 => string 'TICK' (length=4)
      2 => string 'JUM' (length=3)
  1 => 
    array (size=3)
      0 => string 'GA' (length=2)
      1 => int 39
      2 => float 103000000
  2 => 
    array (size=3)
      0 => string 'JT' (length=2)
      1 => int 31
      2 => float 42485000

http://sandbox.onlinephpfunctions.com/code/1646df30b52dc72d522bc86c395b691e1cc634ce

like image 43
n-dru Avatar answered Feb 12 '23 10:02

n-dru