Thursday, September 6, 2012

Sorting Multidimensional array in php

This function helps you to sort multidimensional array based on an particular index in that array

sortmulti($resArray,'INDEXNAME','asc',true,true);

Parameter 1 : It is the multidimensional array.
Parameter 2 : It is the index name in multidimensional array.
Parameter 3 : It is to define the ascending or descending order of index name multidimensional array.Acceptable values(asc,desc).
Parameter 4 : If the index has alphsnumeric values then set this as true else false.
Parameter 5 : If you want index name to case sensistive set this as true.

function sortmulti ($array, $index, $order, $natsort=FALSE, $case_sensitive=FALSE) {
  if(is_array($array) && count($array)>0) {
    foreach(array_keys($array) as $key)
      $temp[$key]=$array[$key][$index];
    if(!$natsort) {
      if ($order=='asc')
      asort($temp);
      else
      arsort($temp);
    }
    else
    {
      if ($case_sensitive===true)
        natsort($temp);
       else
        natcasesort($temp);
      if($order!='asc')
        $temp=array_reverse($temp,TRUE);
    }
    foreach(array_keys($temp) as $key){
      if (is_numeric($key))
        $sorted[]=$array[$key];
      else
        $sorted[$key]=$array[$key];
    }
    return $sorted;
   }
  return $sorted;
}

Wednesday, September 5, 2012

Get all the children from an taxonomy term



You have pass the tid into this function which will return all the child terms under it.
<?php
function taxonomy_get_children_all($tid, $vid = 0, $key = 'tid'){
  $c = taxonomy_get_children($tid, $vid, $key);
  $result = array();
  foreach ($c as $t => $d){
    $result[$t] = $d;
    $below = taxonomy_get_children_all($t, $vid, $key);
    if (!empty($below)) {
      foreach ($below as $nt => $nd){
        $result[$nt] = $nd;
      }
    }
  }
  return $result;
}
?>

Group concat in mysql


Using group concat you can concat an column in an single row.But group concat length which has a default value of 1024.if you want to increase the size you have to use the below query & then execute the group_concat query.

SET group_concat_max_len = 200000;

SELECT GROUP_CONCAT(title) FROM node WHERE nid IS NOT NULL LIMIT 100

Natural sorting in MYSQL or Alphanumeric sorting in mysql

MYSQL is still working on Natural sorting.
Here is some quick solution for natural sorting in MYSQL.
This worked perfectly for me.

SELECT name FROM taxonomy_term_data WHERE ORDER BY name+0<>0 DESC, name+0, name

If you have values in name column like below
   2-test
   Test
   12-test
   22-test
   Ascend

& the Output will be:
   2-test
   12-test
   22-test
   Ascend
   Test