4

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>";
?>
2
  • 2
    Use mysql_fetch_array Commented Jun 18, 2015 at 7:07
  • 4
    Stop using deprecated mysql_* API. use mysqli_*or PDO with prepared statements. Commented Jun 18, 2015 at 7:08

3 Answers 3

5

Changed like this:

$tes = array();
while($row=mysql_fetch_array($result)){
    array_push($tes, $row);
}
Sign up to request clarification or add additional context in comments.

2 Comments

check this one very simple.Thanks @kroseva
sorry for being late reply @Ramki. but it doesn't suited to my case but thanks for your help..
3

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

1 Comment

thanks @n-dru for your help.. as far as i try solution from you guys, this is the closest to what i need..
0

A foreach is also a possibility

foreach ( $tes as $k => $test) {
 $i = 0;
 foreach ( $test as $key => $t ) {
  $tes[$k][$i] = $test[$key];
  unset($tes[$k][$key]);   
  $i++;
 }
}

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.