Jump to content

[SOLVED] array sorting help


nadeemshafi9

Recommended Posts

hi guys i am trying to sort an array with direction and key, my problem is its not working and i think im using the wriong functions.   

 

this is teh structure of teh array

 

        $ddiArray2[$tenant][22]["ext"] = "0000";

    $ddiArray2[$tenant][22]["ddi"] = "00000";

    $ddiArray2[$tenant][22]["rm"] =  "dfgfdgfdg";

 

here is my code

 

        if($dir == "ASC") asort($dataArray[$sort]);

    if($dir == "DESC") arsort($dataArray[$sort]);

 

 

Thanks for any help

Link to comment
Share on other sites

There has to be more to it than that. Your array is $ddiArray2, but you are trying to sort "$dataArray"... ???

 

sorry that was my mistake the dataArray is made out of queries in the db based on the ddi array

 

so after all my work i have a working $dataArray i use it in all sorts of components and its basicaly solid.

 

my component in JS calls back to this method with sort paramiters if it was just a SQL statment it owuld be easy, but its an array $ddiArray2 made out of multiple query results.

 

i basicaly need to sort the array on key and direction

 

the structure of both arrays is teh same

Link to comment
Share on other sites

data array is an array of arrays its built in a loop like this

 

                        $data["id"] = $ddi["ddi"];

    $data["count"] = "0";

    $data["src"] = $ddi["ddi"];

    $data["ext"] = $ddi["ext"];

    $data["rm"] = $ddi["rm"];

    $dataArray[] = $data;

Link to comment
Share on other sites

foreach($ddiArray2[$tenant] as $k => $ddi){

    $where = "(src = '{$ddi["ext"]}' OR src = '{$ddi["ddi"]}') AND calldate >= '{$fromtime}' AND calldate <= '{$totime}' AND billsec > 0 AND LENGTH(dst) > 4";

    $select = $cdr_table->select()->from($cdr_table, array('COUNT(src) as count', "id"))

    ->group('src')

    ->where($where);

       

    $row = $cdr_table->fetchRow($select);

    if(count($row) >0)

    {

    $data = $row->toArray();

    $data["src"] = $ddi["ddi"];

    $data["ext"] = $ddi["ext"];

    $data["rm"] = $ddi["rm"];

    $dataArray[] = $data ;

    }

    else

    {

    $data["id"] = $ddi["ddi"];

    $data["count"] = "0";

    $data["src"] = $ddi["ddi"];

    $data["ext"] = $ddi["ext"];

    $data["rm"] = $ddi["rm"];

    $dataArray[] = $data;

    }

    }

Link to comment
Share on other sites

if($limit == "0"){

    $limit = $totalCount;

    }

 

    if($dir == "ASC") sort($dataArray);

    if($dir == "DESC") rsort($dataArray);

   

$dojoData = new Zend_Dojo_Data('id',array_slice($dataArray, $start, $limit) ,'id');

$dojoData->setMetadata(array('totalCount' => $totalCount));

Link to comment
Share on other sites

HERE IS A DUMP

 

 

Array

(

[0] => Array

(

[id] => 678678

[count] => 0

[src] => 576765

[ext] => 567657

[rm] => gfjf

)

 

[1] => Array

(

[id] => 568769

[count] => 0

[src] => 7487648

[ext] => 5476547

[rm] => fdjfdg

)

 

[2] => Array

(

[id] => 657657

[count] => 0

[src] => 457657

[ext] => 6587657

[rm] => rtytrytry

)

 

}

Link to comment
Share on other sites

PHP provides a range of functions that you can use for sorting data based on the key or value of an associative array. Where it gets complicated is when you have an array of associative arrays which you need to sort according to a particular attribute or combination of attributes.

 

This is a very common task for PHP programmers as data returned from database/SQL queries often appears in this format (ref: pg_fetch_assoc and mysql_fetch_assoc).

Link to comment
Share on other sites

DONE:

 

function compare_rm($a, $b)

{

return strnatcmp($a['rm'], $b['rm']);

}

function compare_ext($a, $b)

{

return strnatcmp($a['ext'], $b['ext']);

}

function compare_src($a, $b)

{

return strnatcmp($a['src'], $b['src']);

}

function compare_count($a, $b)

{

return strnatcmp($a['count'], $b['count']);

}

 

 

 

 

 

if($sort == "rm"){

if($dir == "ASC") usort($dataArray, 'compare_rm');

else if($dir == "DESC") {

usort($dataArray, 'compare_rm');

$dataArray = array_reverse($dataArray);

}

}

else if($sort == "ext"){

if($dir == "ASC") usort($dataArray, 'compare_ext');

else if($dir == "DESC") {

usort($dataArray, 'compare_ext');

$dataArray = array_reverse($dataArray);

}

}

else if($sort == "src"){

if($dir == "ASC") usort($dataArray, 'compare_src');

else if($dir == "DESC") {

usort($dataArray, 'compare_src');

$dataArray = array_reverse($dataArray);

}

}

else if($sort == "count"){

if($dir == "ASC") usort($dataArray, 'compare_count');

else if($dir == "DESC") {

usort($dataArray, 'compare_count');

$dataArray = array_reverse($dataArray);

}

}

 

 

its a bit wishy washy, php should have a function for this

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.