-3

Iam developing an appliacation using CI.I have got a problem and I need a help for that.this is my problem. I have an array generated with php:

Array
(
[0] => Array
    (
        [0] => 3
        [1] => 0
    )

[1] => Array
    (
        [0] => 2
        [1] => 0
    )

[2] => Array
    (
        [0] => 1
        [1] => 246
    )

[3] => Array
    (
        [0] => 0
        [1] => 4528
    )

)

This is the code that genarate above array.

public function get_previous_months_total()
{
    $f = 1;
    $dataset2 = array();
    $result;
    for($i=0;$i<4;$i++){    

            $firstday = mktime(0, 0, 0, date('m')-$f, 1, date('Y'));
            $lastday = mktime(0, 0, 0, date('m')-$i, 0, date('Y'));

            $end = date("Y-m-d", $lastday);
            $start = date("Y-m-d", $firstday);
            $f++;               
            $result = $this->LineChart_Model->get_months_total($start,$end);
            foreach ($result as $return_result ){
                    $dataset2[] =        array($i,int)$return_result['SUM(operation_production)']);

            }

    }   

    $mon = array(array_reverse($dataset2));
    return $mon;
}

Here is the code in the Model.

public function get_months_total($start,$end){

    $sql = "SELECT SUM(operation_production) FROM plant WHERE date BETWEEN '".$start."' AND        '".$end."' ORDER BY operation_id DESC";
    $result = $this->linechart_db->query($sql);
    return $result->result_array();
}

after this I encode this using json_encode which gives me the result below:

var total = [
    [
        3,
        0
    ],
    [
        2,
        0
    ],
    [
        1,
        246
    ],
    [
        0,
        4528
    ]
];

I need to change the order to this:

var total = [
    [
        0,
        0
    ],
    [
        1,
        0
    ],
    [
        2,
        246
    ],
    [
        3,
        4528
    ]
];

please help me on this. I have tried many ways, but none have worked. any help is really appreciated.

6
  • How are you generating the original php array? You should either change the code that generates that array, or sort the array before calling json_encode. Commented Aug 24, 2014 at 14:12
  • If you think about it, this has nothing to do with JSON, because the order of the array matches what it was in PHP. The question then becomes "how to sort a multi-dimensional array in PHP"; if you search for those terms you will probably find lots of existing answers. Commented Aug 24, 2014 at 14:22
  • stackoverflow.com/questions/3804278/… Also - use order in (mysql) query =\ Commented Aug 24, 2014 at 14:34
  • I would have suggested array_multisort() as well, except your second code sample is clearly JS and not PHP. Commented Aug 24, 2014 at 14:37
  • Add that code to your original question - otherwise people won't see it. Commented Aug 25, 2014 at 11:17

1 Answer 1

1

You should be able to use the array_mulisort() function to resolve this issue.

**EDIT:

After further investigation the array_multisort() function will not give you your answer, I apologize.

Since the values of your array are already set, your going to have to manipulate them to get the result you want. Here is what worked for me (in your code just replace the $oldArray variable with the name of your array) :

$replacementArray = array();
$i                = 0;

foreach($oldArray as $array) {
    $newArray =[$i, $array[1]];

    array_push($replacementArray, $newArray);

    $i++;
}

$finalArray = array_replace($oldArray, $replacementArray);
$json       = json_encode($finalArray);

echo $json;

This will give the following output:

[[0,0],[1,0],[2,246],[3,4528]]
Sign up to request clarification or add additional context in comments.

1 Comment

thank you Anth Bieb.but it didn't work for me.still giving me the same result.please check the code above comment section that I have used to generate the PHP array.

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.